简体   繁体   English

wpf mvvm中的复选框问题

[英]Checkbox issues in wpf mvvm

I have the problem when I using CheckBox in my GridView using WPF and MVVM. 当我在使用WPF和MVVM的GridView使用CheckBox ,我遇到了问题。

 <GridViewColumn.CellTemplate>
                  <DataTemplate>
                     <CheckBox IsChecked="{Binding IsSelected}"/>
                   </DataTemplate>
  </GridViewColumn.CellTemplate>

My ViewModel 我的ViewModel

 public bool IsSelected
    {
        get
        {
            return _isSelected;
        }
        set
        {
            if (_isSelected == value)
            {
                return;
            }
            _isSelected = value;
            OnPropertyChanged("IsSelected");
        }
    }

How I can select the data in a selected row, ie the CheckBox value of that row is true? 如何选择所选行中的数据,即该行的CheckBox值是否为真?

You will need to set up a IEnumerable<bool> / IEnumerable<SomeClass> that holds the IsChecked information for each of your CheckBox es. 您需要设置一个IEnumerable<bool> / IEnumerable<SomeClass> ,它包含每个CheckBox es的IsChecked信息。 Something like 就像是

public class CheckedItem
{
    public CheckedItem() { }

    public CheckedItem(string text, bool isChecked) : this()
    {
        this._text = text;
        this._isChecked = isChecked;
    }

    private string _text;
    public String Text;
    {
        get { return _text; }
        set
        {
            if (_text == value)
                return;
            _text = value;
            OnPropertyChanged("IsSelected");
        }
    }

    private bool _isSelected;
    public bool IsSelected;
    {
        get { return _isSelected; }
        set
        {
            if (_isSelected == value)
                return;
            _isSelected = value;
            OnPropertyChanged("IsSelected");
        }
    }
}

private ObservableCollection<CheckedItem> checkItemCollection = 
    new ObservableCollection<CheckedItem>();
public ObservableCollection<CheckedItem> CheckItemCollection
{
    get { return checkItemCollection; }
    set
    {
        if (checkItemCollection == value)
            return;
        checkItemCollection = value;
        OnPropertyChanged("CheckItemCollection");
    }
}

in XAML you can then bind the GridView to this using 在XAML中,您可以使用GridViewGridView绑定到此

<GridView ItemsSource="{Binding Path=CheckedItemCollection, 
                                Mode=TwoWay, 
                                UpdateSourceTrigger=PropertyChanged, 
                                IsAsync=True}" 
    <GridView.Columns>
        <GridViewColumn.CellTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Text}" 
                          IsChecked="{Binding IsSelected, 
                                              Mode=TwoWay, 
                                              UpdateSourceTrigger=PropertyChanged, 
                                              IsAsync=True}"/>
            </DataTemplate>
        </GridViewColumn.CellTemplate>
    </GridView.Columns>
</GridView>

This was done without an IDE so you may have to tweak this to get it to work. 这是在没有IDE的情况下完成的,因此您可能需要对其进行调整才能使其正常工作。

I hope this helps. 我希望这有帮助。

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

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