选中 WPF 的复选框时触发事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14930637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firing an event when checkbox is checked for WPF
提问by anevil
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 CheckBoxitems checked:
获取当前在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>
Thanks a lot :-).
非常感谢 :-)。
回答by Rhexis
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:
编辑:刚刚注意到您将复选框的内容设为“{Binding posID}”,因此您可以执行的操作(因为您有复选框列表)在选中的事件中,例如:
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
这会给你“posID”,你也可以用它做你需要的。:D

