[英]Firing an event when checkbox is checked for WPF
获取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>
非常感谢 :-)。
您可以使用已检查的事件:
<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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.