大家好,我有這個代碼的問題,請幫助我..複選框WPF MVVM
我有看法
<StackPanel Orientation="Horizontal" Margin="3">
<Label Content="Audit Type" MinWidth="100"/>
<Label Content=":"/>
<StackPanel Orientation="Vertical">
<ListBox ItemsSource="{Binding Items}" Margin="3" SelectionMode="Extended" MinWidth="180">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Name="check" Content="{Binding Value}" IsChecked="{Binding IsChecked, Mode=TwoWay}" Margin="3" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</StackPanel>
和視圖模型
private List<AuditTypeExport> _items;
private List<string> _value;
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
OnPropertyChanged("IsChecked");
}
}
public List<AuditTypeExport> Items
{
get { return _items; }
}
public List<string> Value
{
get { return _value; }
set
{
_value = value;
OnPropertyChanged("Value");
}
}
而且視圖模型Constractor
_items = _model.GetAuditType();
_value = _model.GetAuditType().Select(item => item.Name).ToList();
對於您的信息離子
public class AuditTypeExport
{
private int _id;
private string _name;
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
結果:複選框出現,但內容沒有,我不知道爲什麼。
問題2:我想讓價值回來,我該怎麼做?
謝謝
您不需要_value字段/ Value屬性。您應該綁定到AuditExporType的名稱屬性。我沒有看到兩者如何保持同步,或者ListBox如何知道如何協調Items集合和Value集合。 – 2011-03-03 06:22:14
你應該考慮改變你的標籤爲文本塊 - 它們更輕量級 – 2011-03-03 06:34:11