设置RibbonComboBox内容的样式

时间:2016-08-03 23:43:43

标签: c# wpf xaml

我正在学习WPF而且RibbonComboBox控件给了我数周的麻烦。

我似乎终于得到了一些基本的功能。现在我已经陷入了应该是微不足道的事情,但就像WPF的其他部分一样,不是。

以下是我的XAML的一部分:

<RibbonGroup Header="Category">
    <RibbonComboBox Label="Category:" HorizontalContentAlignment="Left" SelectionBoxWidth="250">
        <RibbonGallery ColumnsStretchToFill="True" SelectedItem="{Binding SelectedCategory}">
            <RibbonGalleryCategory DisplayMemberPath="Text" MaxColumnCount="1" ItemsSource="{Binding Categories}">
            </RibbonGalleryCategory>
        </RibbonGallery>
    </RibbonComboBox>
    <RibbonComboBox Label="Subcategory:" HorizontalContentAlignment="Left" SelectionBoxWidth="250">
        <RibbonGallery MaxColumnCount="1" ColumnsStretchToFill="True" SelectedItem="{Binding SelectedSubcategory}">
            <RibbonGalleryCategory DisplayMemberPath="Text" ItemsSource="{Binding Subcategories}">
            </RibbonGalleryCategory>
        </RibbonGallery>
    </RibbonComboBox>
    <RibbonButton Label="Edit Categories" Command="local:EditCommands.Categories" SmallImageSource="Images\categories_sm.png" ToolTipTitle="Edit Categories" ToolTipDescription="Add, edit or delete categories and subcategories" ToolTipImageSource="Images\categories_sm.png"></RibbonButton>
</RibbonGroup>

我遇到的问题是组合框下拉列表的选择部分只与文本一样宽。 (我希望它与完整的下拉一样宽。)

如您所见,我添加了一些MaxColumnCountColumnStretchToFill属性。这些似乎最初有效,但......

  1. 当代码刷新内容时,ColumnStretchToFill设置似乎被丢弃,选择栏再次只与选择文字一样宽。

  2. RibbonComboBoxRibbonGalleryRibbonGalleryCategory的层次结构。我找不到原因。其中不止一个元素具有MaxColumnCountColumnStretchToFill属性(以及其他注释属性)。我如何知道应该为这些属性设置哪个元素?

1 个答案:

答案 0 :(得分:2)

要实现目标,请执行以下操作:

android:icon="@mipmap/ic_launcher"

您必须设置<Ribbon> <RibbonGroup Header="Category" Height="100"> <RibbonComboBox Label="Category:" > <RibbonGallery SelectedItem="{Binding SelectedCategory, Mode=TwoWay, IsAsync=True}" > <RibbonGalleryCategory ItemsSource="{Binding Categories}" DisplayMemberPath="Name" ColumnsStretchToFill="True" MaxColumnCount="1" IsSharedColumnSizeScope="True" /> </RibbonGallery> </RibbonComboBox> <RibbonComboBox Label="Subcategory:" > <RibbonGallery SelectedItem="{Binding SelectedSubCategory}" > <RibbonGalleryCategory ItemsSource="{Binding SelectedCategory.SubCategories}" DisplayMemberPath="Name" ColumnsStretchToFill="True" MaxColumnCount="1" IsSharedColumnSizeScope="True"/> </RibbonGallery> </RibbonComboBox> <RibbonButton Label="Edit Categories" ToolTipTitle="Edit Categories" ToolTipDescription="Add, edit or delete categories and subcategories" Command="{Binding AddCatCommand}"></RibbonButton> </RibbonGroup> </Ribbon> 才能使项目延伸到IsSharedColumnSizeScope="True"。这确保了适当的布局。

上面的示例向您展示了差异,因为我没有在您的第二个ComboBox上设置该属性。

希望这有帮助。

修改

这是一个简单的模型

ComboBox

现在有一些 ViewModel - 我放入了CodeBehind

public class Category {
        private ObservableCollection<Category> _subCats = new ObservableCollection<Category>();
        public string Name { get; set; }

        public ObservableCollection<Category> SubCategories => this._subCats;
    }

<强>清除率

正如您可能注意到的,所使用的集合中没有一个具有setter,从而阻止它们被覆盖。如果使用public partial class Window1 : INotifyPropertyChanged { private Category _selectedCategory; private ObservableCollection<Category> _categories = new ObservableCollection<Category>(); private Category _selectedSubCategory; public Window1() { InitializeComponent(); var cat = new Category() { Name = "Category 1" }; cat.SubCategories.Add(new Category { Name = "Cat 1 - Subcat 1" }); cat.SubCategories.Add(new Category { Name = "Cat 1 - Subcat 2" }); cat.SubCategories.Add(new Category { Name = "Cat 1 - Subcat 3" }); var cat2 = new Category() { Name = "Category 2" }; cat2.SubCategories.Add(new Category { Name = "Cat 2 - Subcat 1" }); cat2.SubCategories.Add(new Category { Name = "Cat 2 - Subcat 2" }); var cat3 = new Category() { Name = "Category 3" }; cat2.SubCategories.Add(new Category { Name = "Cat 3 - Subcat 2" }); this.Categories.Add(cat); this.Categories.Add(cat2); this.Categories.Add(cat3); this.SelectedCategory = this.Categories.First(); this.DataContext = this; } public ICommand AddCatCommand => new RelayCommand(x => { var newCat = new Category { Name = "Im New" }; newCat.SubCategories.Add(new Category { Name = "I'm new too" }); this.Categories.Add(newCat); }); public Category SelectedCategory { get { return _selectedCategory; } set { if (Equals(value, _selectedCategory)) return; _selectedCategory = value; OnPropertyChanged(); } } public Category SelectedSubCategory { get { return _selectedSubCategory; } set { if (Equals(value, _selectedSubCategory)) return; _selectedSubCategory = value; OnPropertyChanged(); } } public ObservableCollection<Category> Categories => this._categories; public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ,则绑定的收藏集应重置,只需清除ItemsSource并添加新项目,因为上例中的按钮会显示