Disable event when checkbox is checked for WPF

What would be the right way to get what is being tested at CheckBox. What I have done so far will not fire any event in CheckBoxchecked elements:

<ListBox Grid.RowSpan="3" Grid.Column="2" Grid.ColumnSpan="5" Margin="2" ItemsSource="{Binding MachinePositionList}">
    <ListBox.ItemTemplate>
        <HierarchicalDataTemplate>
            <CheckBox Content="{Binding posID}" IsChecked="{Binding IsChecked, Mode=TwoWay}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Checked">
                        <i:InvokeCommandAction Command="{Binding CurrentCheckedPosition}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>                           
            </CheckBox>
       </HierarchicalDataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Many thanks: -).

+5
source share
1 answer

You can use the marked events:

<CheckBox Name="myCheckBox" 
          Content="I am a checkbox!" 
          Checked="myCheckBox_Checked" 
          Unchecked="myCheckBox_Unchecked" />

And the code for these events:

private void myCheckBox_Checked(object sender, RoutedEventArgs e)
{
    // ...
}

private void myCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    // ...
}

EDIT: Just noticed that you have the contents for the flags as "{Binding posID}", so something you can do (since you have a list of flags) is in the checked events, there is something like:

if (sender != null)
{
     int posID = Convert.ToInt32(((CheckBox)sender).Name);
}

This will give you a "posID" and you can do what you need .: D

+4
source

All Articles