3

在 View(即 XAML)中,我已将 SelectAll 布尔值绑定到 Checkbox 控件

<CheckBox Grid.Row="5" Content="Select All" HorizontalAlignment="Left" Margin="0,5, 0, 0"  IsChecked="{Binding Path=SelectAll, Mode=TwoWay}" Width="206"></CheckBox>

并将 SelectAll 填充为

public bool SelectAll
    {
        get { return this.Get<bool>("SelectAll"); }
        set 
        {
            this.Set<bool>("SelectAll", value);
            if (this.SelectAllCommand.CanExecute(null))
                this.SelectAllCommand.Execute(value);
        }
    }

是的,它看起来不错,但我有一个问题......

当手动选中所有的复选框时,selectall复选框应该是自动选中的……那个时候,我们不希望执行SelectAllCommand命令……

我该怎么做......

谢谢你提前给我一些建议

4

2 回答 2

1

尝试

public bool? SelectedAll
{
    get { return this.Get<bool?>("SelectedAll"); }
    set 
    {
        if(Equals(SelectedAll, value) == true)
            return;
        this.Set<bool?>("SelectedAll", value);
        OnSelectedAllChanged(value);
    }
}

private void SelectedAllChanged(bool? input)
{
    //Stuff
}
于 2013-04-08T07:19:19.790 回答
0

您需要在任何时候选择项目时使用PropertyChanged项目的事件来重新评估SelectAll值。

例如,

// Setup PropertyChange notification for each item in collection
foreach(var item in MyCollection)
{
    item.PropertyChanged += Item_PropertyChanged;
}

private bool _isSelectAllExecuting = false;

// If the IsSelected property of an item changes, raise a property change 
// notification for SelectAll
void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (!_isSelectAllExecuting && e.PropertyName == "IsSelected") 
        ReevaluateSelectAll();
}

void ReevaluateSelectAll()
{
    // Will evaluate true if no items are found that are not Selected
    _selectAll = MyCollection.FirstOrDefault(p => !p.IsSelected) == null;

    // Since we're setting private field instead of public one to avoid
    // executing command in setter, we need to manually raise the
    // PropertyChanged event to notify the UI it's been changed
    RaisePropertyChanged("SelectAll");
}

void SelectAll()
{
    _isSelectAllExecuting = true;

    foreach(var item in MyCollection)
        item.IsSelected = true;

    _isSelectAllExecuting = false;
}
于 2013-04-08T15:36:05.863 回答