简体   繁体   English

复选框WPF MVVM

[英]Checkbox WPF MVVM

Hi all i have problem in this code, please help me.. 大家好,我在此代码中有问题,请帮助我..

I have view 我有看法

<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>

and for View model 对于View模型

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");
        }
    }

And ViewModel Constractor 和ViewModel承包商

_items = _model.GetAuditType();
_value = _model.GetAuditType().Select(item => item.Name).ToList();

For your information 供你参考

public class AuditTypeExport
{
    private int _id;
    private string _name;

    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }

The result : checkbox appeares, but the content doesn't and I don't have a clue why. 结果:出现复选框,但内容不存在,我不知道为什么。

Question Number 2 : I want to get the value back, how can I do that? 问题2:我想找回价值,我该怎么做?

Thank you 谢谢

It is unclear how you are using your ViewModel. 目前尚不清楚您如何使用ViewModel。 Is that bound to the form? 那绑定到表格了吗? Or each item in the ListBox? 还是列表框中的每个项目?

It looks like your ListBox is bound to the Items collection of your VM, so the ItemTemplate will be used with a AuditTypeExport as the data context. 看来您的ListBox已绑定到VM的Items集合,因此ItemTemplate将与AuditTypeExport一起用作数据上下文。 You are binding to "Value" and "IsChecked" properties which do not exist on the AuditTypeExport class. 您将绑定到AuditTypeExport类上不存在的“ Value”和“ IsChecked”属性。

What you are trying to do here is bind a property of type List<String> Value to CheckBox 's Content property which is of type Object . 您在此处尝试执行的操作是将List<String> Value类型的属性绑定到CheckBoxObject类型的Content属性。

To simplify, you are assigning a collection of strings to a string. 为简化起见,您正在将字符串集合分配给字符串。 Which is not a good thing. 这不是一件好事。 And that is why it does not work. 这就是为什么它不起作用的原因。

Try using ItemsControl to show Value property or use an IValueConverter to convert List<String> to comma separated string. 尝试使用ItemsControl显示Value属性,或使用IValueConverterList<String>转换为逗号分隔的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM