0

如何制作CheckEdit全选。

Xaml 自动生成checkEdit

   <ItemsControl ItemsSource="{Binding MyProperty, Mode=TwoWay}" Margin="0"  Grid.Column="1" Grid.RowSpan="1" x:Name="MyCheck" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <dxe:CheckEdit Content="{Binding FilePath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Padding="2.5" Margin="3"  
                               IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical"  />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

        <Button x:Name="filtrButton" Content="Filtr" Command="{Binding FiltrCommand}"  Padding="5" Margin="3" IsEnabled="{Binding IsEnabled}"/>
        <dxe:CheckEdit x:Name="CheckALL" Content="Select All" Padding="2.5" Margin="3" IsChecked="{Binding AllSelected}" />

C#

        private bool? _allSelected = true;
    public bool? AllSelected
    {
        get { return _allSelected; }
        set
        {
            _allSelected = value;

          MyProperty.ForEach(x => x.IsChecked = value);

            OnPropertyChange("AllSelected");
        }
    }
    public List<MyCheckBox> MyProperty
    {
        get { return TempList; }
        set
        {
            TempList = value;
            OnPropertyChange("MyProperty");
        }
    }
/// добавление название файла и его select
        public ViewModel(){
       TempList = new List<MyCheckBox>();

        foreach (var type in tempUniqueList)
        {
            TempList.Add(new MyCheckBox(type, _allSelected));
        }

        MyProperty = TempList;
    }
4

1 回答 1

0

_allSelected 是不必要的,因为它的状态来自于其他项目是否被选中。(注意:这与选择不同,这意味着 ItemsControls 中的其他内容)

public bool? AllSelected
{
    get 
    { 
        // Check if all are checked
        if (!MyProperty.Any(x => !x.IsChecked))
        {
            return true;
        }

        // Check if none are checked
        if (!MyProperty.Any(x => x.IsChecked))
        {
            return false;
        }

        // Otherwise some are checked.
        return null;
    }

    set
    {
        _allSelected = value;

        MyProperty.ForEach(x => x.IsChecked = value);

        OnPropertyChange("AllSelected");
    }
}

现在,另一件重要的事情是,只要其中一项的 IsChecked() 值发生更改,您就需要提高OnPropertyChanged("AllSelected") 。在这一点上,这不是一个很好的解决方案。您需要为插入“MyProperty”的每个项目挂钩PropertyChanged 事件,测试它是否是IsChecked属性,如果是然后引发PropertyChanged。

就个人而言,我倾向于将这类事情更多地视为视图的一部分,而不是视图模型。我的意思是,从概念上讲,“全选/全选”复选框实际上是 ItemsControl 的扩展。因此,您可能完全有理由在代码隐藏中执行此操作,并且只需连接到 DataTemplate 中的 Checked() 事件。

于 2015-08-17T13:18:43.307 回答