jeudi 3 août 2017

Prevent a onchecked event in CheckBox while scrolling it in ListBox

I have a list of CheckBox in a ListBox, and the ListBox can scroll, but every time I move up or down in the ListBox to see the other CheckBoxes the Checked event fires. Is there any way to prevent it? I just wanted that event to be fired when the IsChecked is changed!

<ListBox Margin="0,5,0,0" Grid.Column="1" Background="Transparent" 
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             ScrollViewer.VerticalScrollBarVisibility="Visible" 
             SelectionMode="Single" Name="lbPermission" BorderThickness="0"
             HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsEnabled="{Binding Path=IsEditable, Mode=OneWay, 
                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}}}" 
                              Validation.ErrorTemplate="{x:Null}" Content="{Binding Id}" 
                              Checked="CheckedHandler" Unchecked="CheckedHandler" >
                        <CheckBox.IsChecked>
                            <MultiBinding Converter="{StaticResource EnumConverter}" 
                                           Mode="OneWay">
                                <Binding Path="Id" />
                                <Binding Path="CurrentAccessList" 
                                         RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Expander}}" />
                            </MultiBinding>
                        </CheckBox.IsChecked>
                    </CheckBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

I tried to make it work with TowWay, or using PropertyChanged, but it didn't.

Here is the code for the event:

private void CheckedHandler(object sender, RoutedEventArgs e)
    {
        RaiseEvent(new RoutedEventArgs(OnDataModifiedEvent));
    }

Thanks.




Aucun commentaire:

Enregistrer un commentaire