选中WPF复选框时触发事件 [英] Firing an event when checkbox is checked for WPF

查看:2811
本文介绍了选中WPF复选框时触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

非常感谢:-).

推荐答案

您可以使用选中的事件:

You can use the checked events:

<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}",因此在选中的事件中您可以执行的操作(因为您具有复选框列表),例如:

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);
}

这将为您提供"posID",您也可以使用它来完成所需的操作. :D

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

这篇关于选中WPF复选框时触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆