我正在combobox
使用MVVM
模式将 Observable 集合绑定到combobox
aSelectedItem
我想象它的方式是,必须有某种方法来创建XAML
指向所选项目的绑定,并且我以后可以在我的视图模型中使用它。我似乎无法弄清楚的是如何......
关于如何实现这一目标的任何建议?
XAML
<ComboBox SelectedIndex="0" DisplayMemberPath="Text" ItemsSource="{Binding Path=DocumentTypeCmb,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Grid.Column="1" Grid.Row="4" Margin="0,4,5,5" Height="23"
HorizontalAlignment="Left" Name="cmbDocumentType" VerticalAlignment="Bottom"
Width="230" />
代码
//Obesrvable collection property
private ObservableCollection<ListHelper> documentTypeCollection = new ObservableCollection<ListHelper>();
public ObservableCollection<ListHelper> DocumentTypeCmb
{
get
{
return documentTypeCollection;
}
set
{
documentTypeCollection = value;
OnPropertyChanged("DocumentTypeCmb");
}
}
//Extract from the method where i do the binding
documentTypeCollection.Add(new ListHelper { Text = "Item1", IsChecked = false });
documentTypeCollection.Add(new ListHelper { Text = "Item2", IsChecked = false });
documentTypeCollection.Add(new ListHelper { Text = "Item3", IsChecked = false });
DocumentTypeCmb = documentTypeCollection;
//Helper class
public class ListHelper
{
public string Text { get; set; }
public bool IsChecked { get; set; }
}