Wpf 我想捕获Datatemplate中定义的已检查事件

Wpf 我想捕获Datatemplate中定义的已检查事件,wpf,xaml,Wpf,Xaml,我有一个ResourceDictionary,其中有以下代码: <DataTemplate x:Key="UserDataTemplate"> <StackPanel Orientation="Horizontal"> <StackPanel> <Image Source="{Binding Path=MyPhoto}" Height="100"/>

我有一个ResourceDictionary,其中有以下代码:

<DataTemplate x:Key="UserDataTemplate">
        <StackPanel Orientation="Horizontal">
            <StackPanel>
                <Image Source="{Binding Path=MyPhoto}" Height="100"/>
                <Label HorizontalAlignment="Center" Content="{Binding Path=Names}"/>
            </StackPanel>
            <CheckBox x:Name="chkBxRegistered" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </StackPanel>        
    </DataTemplate>
在我的xaml文件中,我有:

<ItemsControl x:Name="UserList" ItemTemplate="{StaticResource UserDataTemplate}" Grid.Column="1">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <VirtualizingStackPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
我用这种方式把我的名单绑起来

UserList.ItemsSource=结果//列表结果
因此,在我的代码隐藏中,我想捕捉checked事件并执行我需要的所有逻辑,或者如果有人有其他想法,请发表评论并帮助我,感谢并对我的英语不好表示歉意。

考虑到我不知道你的xaml.cs是什么样子,解决此问题的最简单方法是将DataTemplate从ResourceDictionary移动到xaml文件,在该文件中定义了ItemsControl,然后在DataTemplate中的复选框上设置Checked事件处理程序

修改后的数据模板如下所示:

<DataTemplate x:Key="UserDataTemplate">
    <StackPanel Orientation="Horizontal">
        <StackPanel>
            <Image Source="{Binding Path=MyPhoto}" Height="100"/>
            <Label HorizontalAlignment="Center" Content="{Binding Path=Names}"/>
        </StackPanel>
        <CheckBox x:Name="chkBxRegistered" VerticalAlignment="Center" HorizontalAlignment="Center" Checked="ChkBxRegistered_OnChecked"/>
    </StackPanel>
</DataTemplate>

您能否不为复选框上的IsChecked和Command属性使用绑定来实现您正在尝试的功能?检查已接受的答案。问题是我没有使用MVVM:我明白了,您在DataTemplate中使用绑定,您可以在当前有MyPhoto的任何位置拥有IsChecked和Command Binding的属性,名称属性。你的意思是这样的:不。类似这样的东西:还有,把你的代码贴在你有名称的地方,还有MyPhoto定义的xaml.cs?
private void ChkBxRegistered_OnChecked(object sender, RoutedEventArgs e)
{
    // Your checked handling code goes here
}