Search code examples
c#wpfxamlmvvmdevexpress-wpf

WPF, C# - SelectAll in CheckEdit


how to make CheckEdit Select All.

Xaml automatically generate 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;
    }

Solution

  • _allSelected is unnecessary, because it's state comes from whether the other items are checked or not. (Note: This is different to selection, which means something else in 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");
        }
    }
    

    Now, the other important thing is that you need to raise OnPropertyChanged("AllSelected") whenever the IsChecked() value for one of the items is changed. This is not a very nice solution at this point. You need to hook the PropertyChanged event for each item inserted into "MyProperty", test whether it is the IsChecked property, and if it is then raise PropertyChanged.

    Personally, I tend to think of this sort of thing as more part of the View, rather than the View Model. I mean, a "Select All/Select None" checkbox is really an extension of the ItemsControl, conceptually. So you would probably be fully justified to do this in code-behind and just hook up to the Checked() events in your DataTemplate.