C# 复选框WPF MVVM
大家好,我在这个代码中有问题,请帮助我 我有自己的看法C# 复选框WPF MVVM,c#,wpf,mvvm,C#,Wpf,Mvvm,大家好,我在这个代码中有问题,请帮助我 我有自己的看法 <StackPanel Orientation="Horizontal" Margin="3"> <Label Content="Audit Type" MinWidth="100"/> <Label Content=":"/> <StackPanel Orientation="Vertical">
<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>
供参考
public class AuditTypeExport
{
private int _id;
private string _name;
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
结果:复选框出现了,但是内容没有,我也不知道为什么
问题2:我想拿回这个值,我该怎么做
谢谢不清楚您是如何使用ViewModel的。这和表格有关系吗?还是列表框中的每个项目
看起来您的列表框已绑定到VM的Items集合,因此ItemTemplate将与AuditTypeExport一起用作数据上下文。您正在绑定到AuditTypeExport类上不存在的“Value”和“IsChecked”属性。您在这里尝试的是将类型为
列表值的属性绑定到类型为对象的复选框的内容属性
为了简化,将字符串集合指定给字符串。这不是一件好事。这就是它不起作用的原因
尝试使用ItemsControl
显示Value
属性,或使用IValueConverter
将List
转换为逗号分隔的字符串。您不需要\u Value字段/Value属性。您应该绑定到AuditExportType的name属性。我不知道这两个是如何保持同步的,或者ListBox会知道如何协调项目集合和值集合。您应该考虑将标签更改为文本块——它们在MVVM中更为轻量级,因此我感到困惑。只是从某人那里获取代码并尝试一下。在mycode中,它假设ItemTemplate获取列表,值从ViewModel-->AuditTypeExport.Name获取,并选中从用户获取使用它。是吗?这不是一个可以简单回答的问题。您需要正确地学习MVVM。
_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; }