我正在学习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>
我遇到的问题是组合框下拉菜单的选择部分仅与文本一样宽。 (我希望它与整个下拉菜单一样宽。)
如您所见,我添加了一些
MaxColumnCount
和ColumnStretchToFill
属性。这些最初似乎有效,但是...当代码刷新内容时,
ColumnStretchToFill
设置似乎被丢弃,选择栏再次仅与选择文本一样宽。有
RibbonComboBox
,RibbonGallery
和RibbonGalleryCategory
的层次结构。我不知道为什么。这些元素中有多个具有MaxColumnCount
和ColumnStretchToFill
属性(以及其他注释属性)。我如何知道应为哪个元素设置这些属性? 最佳答案
为了实现您的目标,请执行以下操作:
<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
上设置该属性。希望这可以帮助。
编辑
这是一个简单的模型
public class Category {
private ObservableCollection<Category> _subCats = new ObservableCollection<Category>();
public string Name { get; set; }
public ObservableCollection<Category> SubCategories => this._subCats;
}
现在我在CodeBehind中放了一些ViewModel的东西
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
,则不应重置绑定的Collection,只需清除.Clear()
,在上述示例中作为Button添加的新项目将向您显示关于c# - 样式RibbonComboBox内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38755347/