如何设置使用SelectedItem
模式?RibbonComboBox
MVVM
看法
<ribbon:RibbonComboBox>
<ribbon:RibbonGallery SelectedItem="{Binding Foobar, Mode=TwoWay}">
<ribbon:RibbonGalleryCategory ItemsSource="{Binding Foobars}" DisplayMemberPath="FoobarID" />
</ribbon:RibbonGallery>
</ribbon:RibbonComboBox>
视图模型
// Selected Item
private Foobar _foobar { get; set; }
public Foobar Foobar
{
get { return _foobar; }
set
{
if (value == _foobar || value == null)
return;
_foobar = value;
base.NotifyPropertyChanged("Foobar");
}
}
// Collection
private ObservableCollection<Foobar> _foobars = new ObservableCollection<Foobar>();
public ObservableCollection<Foobar> Foobars
{
get
{
return _foobars;
}
}
// Constructor
public FoobarViewModel(MyObject myObject)
{
LoadFoobars();
Foobar = myObject.Foobar;
}
// Method
private void LoadFoobars()
{
foreach (var foobar in _localRepository.GetFoobars())
{
this._foobars.Add(foobar);
}
}
更新
删除IsEditable="True"
确实将“Namespace.Foobar”放入RibbonComboBox
并更改SelectedItem
和添加确实显示SelectedValuePath
了RibbonGallery
正确的值,但是RibbonComboBox
有一个红色边框,所以我猜它没有经过验证(比如比较苹果和梨)。
<ribbon:RibbonComboBox>
<ribbon:RibbonGallery SelectedItem="{Binding Foobar.FoobarID, Mode=TwoWay}" SelectedValuePath="DisplayMemberPath">
<ribbon:RibbonGalleryCategory ItemsSource="{Binding Foobars}" DisplayMemberPath="FoobarID"/>
</ribbon:RibbonGallery>
</ribbon:RibbonComboBox>