如何在wpf的DataTemplate中为复选框附加检查事件?

时间:2022-05-22 19:37:12

Hi I am having a CheckBox defined in a DataTemplate which is defined in resource.Xaml file. I am using this DataTemplate in my user control. I am adding this DataTemplate dynamically to a GridView. Now I want to fire the checked event of a CheckBox. How will I attach the event? My Xaml is like this:

你好,我在DataTemplate中定义了一个复选框,该复选框在参考资料中定义。Xaml文件。我正在我的用户控件中使用这个DataTemplate。我将这个DataTemplate动态地添加到GridView中。现在我要启动复选框的已选中事件。我如何附加事件?我的Xaml是这样的:

<ListView>
    <ListView.View>
        <GridView></GridView>
    </ListView.View>
</ListView>

1 个解决方案

#1


1  

Use the fact that the CheckBox.Checked event bubbles up to its parent controls:

使用复选框这一事实。检查过的事件泡泡到它的父控件:

<ListView CheckBox.Checked="YourCheckedEventHandler">
    ...
</ListView>

Obviously you'll have to check the item whose CheckBox was checked in the event handler. The easiest way to do that is to look at the DataContext of the sender parameter, or possibly the Source property on the "e" parameter.

显然,您必须在事件处理程序中选中复选框的项。最简单的方法是查看发送方参数的DataContext,或者可能是“e”参数的源属性。

#1


1  

Use the fact that the CheckBox.Checked event bubbles up to its parent controls:

使用复选框这一事实。检查过的事件泡泡到它的父控件:

<ListView CheckBox.Checked="YourCheckedEventHandler">
    ...
</ListView>

Obviously you'll have to check the item whose CheckBox was checked in the event handler. The easiest way to do that is to look at the DataContext of the sender parameter, or possibly the Source property on the "e" parameter.

显然,您必须在事件处理程序中选中复选框的项。最简单的方法是查看发送方参数的DataContext,或者可能是“e”参数的源属性。