2
如何使用MVVM
模式將SelectedItem
設置爲RibbonComboBox
?如何使用MVVM在RibbonComboBox上設置SelectedItem?
查看
<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
並在RibbonGallery
上增加SelectedValuePath
確實顯示正確的值,但RibbonComboBox
有一個紅色邊框,所以我猜測它沒有驗證(如比較蘋果和梨)。
<ribbon:RibbonComboBox>
<ribbon:RibbonGallery SelectedItem="{Binding Foobar.FoobarID, Mode=TwoWay}" SelectedValuePath="DisplayMemberPath">
<ribbon:RibbonGalleryCategory ItemsSource="{Binding Foobars}" DisplayMemberPath="FoobarID"/>
</ribbon:RibbonGallery>
</ribbon:RibbonComboBox>