mardi 19 avril 2016

Trying to get a ListBox of CheckBoxes to group and have a 'Select All' button

I am trying to create a search menu in my WPF application. So, naturally I have a bunch of different search criteria in 4 different ListBoxes. I am having a hard time with configuring a 'Select All' Checkbox to uncheck all of the other CheckBoxes in the ListBox.

Here is a sample of one of the ListBoxes:

    <StackPanel x:Name="RegionSelection" Orientation="Vertical" Grid.Row="0" Grid.Column="4">
        <TextBlock Style="{DynamicResource LabelTextblockStyle}"
       VerticalAlignment="top" HorizontalAlignment="Left" 
               Margin="4,4,4,4">Regions:</TextBlock>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left">

            <ListBox  Margin="4,0" HorizontalAlignment="Left" ItemsSource="{Binding Path=RegionTypes, Mode=TwoWay}" ItemContainerStyle="{DynamicResource NoHighlightList}">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="2"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                            <CheckBox Content="{Binding Path=RegionDesc}" Command="{Binding Path=UncheckAllCommand}" Margin="4" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </StackPanel>

Here is the ViewModel:

     private List<SearchRegion> _regionTypes;
    public List<SearchRegion> RegionTypes
    {
        get { return _regionTypes; }
        set
        {
            _regionTypes = value;
            RaisePropertyChanged();
        }
    }
    RegionTypes = new List<SearchRegion>
        {   new SearchRegion {RegionDesc = "All Regions"},
            new SearchRegion {RegionDesc = "Region 4", Region="4"},
            new SearchRegion {RegionDesc = "Region 1", Region="1"},
            new SearchRegion {RegionDesc = "Region 5", Region="5"},
            new SearchRegion {RegionDesc = "Region 2", Region="2"},
            new SearchRegion {RegionDesc = "Region 6", Region="6"},
            new SearchRegion {RegionDesc = "Region 3", Region="3"}
        };

And the Command:

    UncheckAllCommand = new RelayCommand(UncheckAll);
    public ICommand UncheckAllCommand {get; private set;}
     private void UncheckAll()
    {
        if(RegionTypes.Any((x) => x.RegionDesc.ToUpper().Equals("ALL REGIONS") && x.IsChecked))
        {
            RegionTypes.ForEach((x) => x.IsChecked = x.RegionDesc.ToUpper() != "ALL REGIONS" ? false : true);
        }
    }

Unfortunately, its just not unchecking the rest of the ListBox. Any help would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire