C# 设置RibbonComboBox内容的样式
我正在学习WPF,而C# 设置RibbonComboBox内容的样式,c#,wpf,xaml,C#,Wpf,Xaml,我正在学习WPF,而RibbonComboBox控件让我头疼了好几个星期 我似乎终于有了一些基本的功能。现在,我被应该是琐碎的东西所困扰,但是,像WPF的其他部分一样,不是 以下是我的XAML的一部分: <RibbonGroup Header="Category"> <RibbonComboBox Label="Category:" HorizontalContentAlignment="Left" SelectionBoxWidth="250">
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>
我遇到的问题是,组合框下拉列表的选择部分仅与文本一样宽。(我希望它与完整下拉列表一样宽。)
如您所见,我添加了一些MaxColumnCount
和ColumnStrechToFill
属性。这些最初似乎有效,但
columnStrechtofill
设置将被丢弃,并且选择栏的宽度也仅与选择文本的宽度相同RibbonComboBox
、RibbonGallery
和RibbonGalleryCategory
的层次结构。我不知道为什么。这些元素中有多个具有MaxColumnCount
和ColumnStrechToFill
属性(以及其他注释属性)。我如何知道应该为哪个元素设置这些属性要实现目标,请执行以下操作:
<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>
现在,一些视图模型——我放在代码背后的东西
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));
}
}
公共部分类窗口1:INotifyPropertyChanged{
私有类别_selectedCategory;
私有ObservableCollection_categories=新ObservableCollection();
私有类别\u选择的子类别;
公共窗口1(){
初始化组件();
var cat=new Category(){Name=“Category 1”};
cat.SubCategories.Add(新类别{Name=“cat1-subcat1”});
cat.SubCategories.Add(新类别{Name=“cat1-subcat2”});
cat.SubCategories.Add(新类别{Name=“cat1-subcat3”});
var cat2=new Category(){Name=“Category 2”};
cat2.SubCategories.Add(新类别{Name=“cat2-subcat1”});
cat2.SubCategories.Add(新类别{Name=“cat2-subcat2”});
var cat3=new Category(){Name=“Category 3”};
cat2.SubCategories.Add(新类别{Name=“cat3-subcat2”});
本.Categories.Add(cat);
本.类别.添加(第2类);
本.类别.添加(cat3);
this.SelectedCategory=this.Categories.First();
this.DataContext=this;
}
public ICommand AddCatCommand=>new RelayCommand(x=>{
var newCat=新类别{Name=“Im new”};
添加(新类别{Name=“我也是新的”});
此.Categories.Add(newCat);
});
公共类别已选择类别{
获取{return\u selectedCategory;}
设置{
如果(等于(值,_selectedCategory))返回;
_selectedCategory=值;
OnPropertyChanged();
}
}
公共类别选定子类别{
获取{return\u selectedSubCategory;}
设置{
如果(等于(值,_selectedSubCategory))返回;
_selectedSubCategory=值;
OnPropertyChanged();
}
}
公共可观察收集类别=>此类别;
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null){
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
清除
正如您可能注意到的,使用的集合中没有一个具有setter,从而防止它们被重写。如果使用了
ItemsSource
,则绑定的集合应该不重置,只需清除.Clear()
,在上面的示例中,作为按钮添加的新项目将向您显示不确定为什么我需要设置三个属性以使它们占据整个宽度。但是,虽然您的更改会这样做,但在我为ItemsSource
提升OnPropertyChanged
并刷新列表后,高亮显示栏不再占用整个宽度。所以它似乎没有帮助。你能添加一些viewmodel的东西吗?所以我可以繁殖得更好。我也不能重现你描述的行为,我可以试着把一些东西放在一起,但是现在有相当多的代码,它们都没有完成,都在转换中。为了澄清这一点,如果您添加了一个运行代码的按钮来设置新的ItemsSource
,高亮显示栏仍然占据下拉列表的整个宽度?(请注意,在我的原始XAML中,我正在为我的RibbonComboBox
设置HorizontalContentAlignment
和SelectionBoxWidth
属性)不要覆盖您的项目资源。清除它并添加新项目。重写ItemsSource将破坏绑定。你说的重写是什么意思?我正在将其绑定到视图模型。但是当“类别”下拉列表中的选定项发生更改时,我必须为子类别设置一个新值。为此,我重置视图模型的子类别列表,然后对该列表执行OnPropertyChanged
。
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));
}
}