在选中WPF复选框时触发事件

时间:2013-02-18 06:28:31

标签: c# wpf events checkbox

获取CheckBox中当前正在检查的内容的正确方法是什么。到目前为止我所做的事情不会在CheckBox项检查中发生任何事件:

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

非常感谢: - )。

1 个答案:

答案 0 :(得分:4)

您可以使用已检查的事件:

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

这些事件的代码是:

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

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

编辑: 只是注意到你有复选框的内容为“{Binding posID}”所以你可以做的事情(因为你有一个复选框列表)在已检查的事件中,有类似的东西:

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

这会给你“posID”,你也可以用它来做你需要的。 :d