Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firing an event when checkbox is checked for WPF

What will be the correct way to get what are currently being checked in the CheckBox. What i have done so far will not firing any event on CheckBox items checked:

<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>

Thanks a lot :-).

like image 517
anevil Avatar asked Feb 18 '13 06:02

anevil


1 Answers

You can use the checked events:

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

And the code for these events is:

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

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

EDIT: Just noticed you have the content for the checkboxes as "{Binding posID}" so something you can do (as you have a list of check boxes) is in the checked events, have something like:

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

This will give you the "posID" and you can do what you need too with it. :D

like image 97
Rhexis Avatar answered Oct 29 '22 11:10

Rhexis