0

I wanted to change the borderBrush if one of the checkbox in the itemscontrol is checked. But i just don't know how to set it. How can i refer to the IsChecked property of the checkbox in the border trigger ?

Thank's for help !

This is my xaml code:

<Border Margin="5"                     
                Grid.Row="1"
                Grid.Column="1"
                Grid.ColumnSpan="3"
                BorderThickness="1"
                >
            <Border.Style>
                <Style TargetType="{x:Type Border}">
                    <Setter Property="BorderBrush" Value="LightGray"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=CheckBox, Path=IsChecked}" Value="False">
                            <Setter Property="BorderBrush" Value="Red"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=CheckBox, Path=IsChecked}" Value="True" >
                            <Setter Property="BorderBrush" Value="LightGray"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <ScrollViewer Height="100">
            <ItemsControl Margin="5"
                          ItemsSource="{Binding AimList, Mode=OneWay}"
                          IsEnabled="{Binding IsEnable}"
                          >
                        <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                            <UniformGrid Margin="5" Columns="2" />
                        </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Name="CheckBox"
                                      Content="{Binding Lib, Mode=OneWay}" 
                                      IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                      Margin="5"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </Border>
  • I assume it is "if any item is checked". If my understanding is wrong and you are actually talking about "if one specific item is checked", please let me know. – kennyzx Jun 23 '17 at 06:25

1 Answers1

0

(when) one of the checkbox in the itemscontrol is checked.

The value you want to bind to is something about a collection, not a specific Checkbox.

You can create a Boolean property in the DataContext, this value is true when the IsChecked property of any item in the AimList is true. And then bind to this property, instead of a Checkbox's IsChecked property.

public Boolean IsAnyAimChecked
{
    get { return AimList.Any(a=>a.IsChecked);} //returns true when any aim is checked
}

Now it comes to the hard part. When any item's IsChecked property is changed, there is no way this IsAnyAimChecked property gets notified of the change, and so its value will not get re-evaluated - so the UI will not get refreshed.

This solution assumes the AimList is an ObservableCollection.

You can register a handler to each Aim item's PropertyChanged event, and notify the change of the IsAnyAimChecked property when any Aim's item's PropertyChanged event is fired.

So when user checks/unchecks an Checkbox, IsAnyAimChecked is re-evaluated, and the UI is reflected after picking up the new value of IsAnyAimChecked.

Please refer to this answer about implementing the propagation of the PropertyChanged event.

kennyzx
  • 12,845
  • 6
  • 39
  • 83