0

您好,任何人都可以在我的代码中帮助我,

我有 xaml:

<ListView Name="__Listview" ItemsSource="{Binding Path=DisplayItems}">
    <CheckBox Content="{Binding Items}" IsChecked="{Binding Path=IsChecked, Mode=TwoWay, NotifyOnTargetUpdated=True}" Checked="CheckBox_Checked" Unchecked="CheckBox_Checked"/>
</ListView>
<CheckBox Name="_checkBoxSelectAll" Checked="CheckBoxToCheckAll" Unchecked="CheckBoxToCheckAll"/>

代码 C#:

public partial class DisplayItems
{     
    private ObservableCollection<Records> _displayItems = new ObservableCollection< Records>();

    public DisplayItems ()
    {
        InitializeComponent();
        _ Listview.DataContext = this;
    }

    public ObservableCollection< Records > DisplayItems
    {
        get { return _displayItems; }
        set { _displayItems = value; }
    }

    private void CheckBoxToCheckAll(object sender, RoutedEventArgs e)
    {          
        if (_checking || !(sender is CheckBox))
            return;

        CheckBox checkBox = (CheckBox)sender;
        _checking = true;

        foreach (Records swElement in DisplayItems)
        {
            bool val;
            if (checkBox.IsChecked != null)
                val = checkBox.IsChecked.Value;
            else
                val = false;

            swElement.IsChecked = val;
        }
        _checking = false;          
    }

    private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        if ((sender as CheckBox) == null || ((sender as CheckBox).DataContext as Records) == null || _checking)
            return;

        _checking = true;

        if (_checkBoxSelectAll.IsChecked != null && _listTo.All(select => select.IsChecked) && !_checkBoxSelectAll.IsChecked.Value)
            _checkBoxSelectAll.IsChecked = true;
        else if (_checkBoxSelectAll.IsChecked == null || _checkBoxSelectAll.IsChecked.Value)
            _checkBoxSelectAll.IsChecked = false;

        _checking = false;
    }
} 

Public class Records
{ 
    public event PropertyChangedEventHandler PropertyChanged;
    private bool _isChecked = false;

    Public Records(){}
    public bool IsChecked
    {
        get { return _isChecked; }
        set { _isChecked = value; OnPropertyChanged("IsChecked"); }
    }

    protected void OnPropertyChanged(string name)
    {
        if ((PropertyChanged != null) && (_notification))
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
    public enum Items{One,Two,three,}
}

如果我检查 _checkBoxSelectAll ,Listview 中的所有复选框都应该检查,我的问题是其 IsChecked=true 背后的代码,但在 UI 中看不到 Checkbox 被选中,请提前帮助我

4

1 回答 1

0

在记录上实施 INotifyPropertyChanged?

Public class Records**: INotifyPropertyChanged**

请用以下替换您的记录类

Public class Records : INotifyPropertyChanged
{ 
public event PropertyChangedEventHandler PropertyChanged;
private bool _isChecked = false;

Public Records(){}
public `bool IsChecked`
        {
            get { return _isChecked; }
            set { _isChecked = value; OnPropertyChanged("IsChecked"); }
        }
protected void OnPropertyChanged(string name)
        {
            if ((PropertyChanged != null) && (_notification))
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
public enum Items{One,Two,three,}
}
于 2013-05-13T16:08:42.250 回答