DataTemplate 单击事件不适用于 UserControl

DataTemplate click event not work on UserControl

我用 2 个按钮为我的列表视图创建了数据模板,如果我在 Window 中使用我的数据模板,则用户必须单击这些按钮,一切正常并且工作正常,但是如果我在 Usercontrol 中使用我的数据模板,我的按钮点击事件就不行工作有什么问题吗?这是我的代码:

<ListView Name="lvDataBinding" HorizontalContentAlignment="Stretch" BorderThickness="0" Margin="10" Grid.Row="3" Background="{x:Null}" SelectionChanged="lvDataBinding_SelectionChanged">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Border  Background="#f0f4f7">
                        <StackPanel Background="#f5f6fa" Margin="1,1,1,1" VerticalAlignment="Top">
                            <Border Background="#edf0f5" BorderThickness="5">
                                <Grid  Background="#ffffff" Height="30">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <StackPanel Background="#ffffff" Margin="5" Orientation="Horizontal">
                                        <Button Height="20" Width="20" BorderBrush="Transparent" BorderThickness="0" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.cmddelete}">
                                            <Button.Background>
                                                <ImageBrush ImageSource="E:\Aks\ICON\colorful-stickers-icons-set\pngx32\accept.png"/>
                                            </Button.Background>
                                        </Button>
                                        <Button Height="20" Width="20" BorderBrush="Transparent" BorderThickness="0" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.cmdShow}">
                                            <Button.Background>
                                                <ImageBrush ImageSource="E:\Aks\ICON\colorful-stickers-icons-set\pngx32\accept.png"/>
                                            </Button.Background>
                                        </Button>
                                    </StackPanel>
                                    <TextBlock Name="txtPhon" Foreground="#7c7f84" HorizontalAlignment="Right" Grid.Column="1" Text="{Binding Path=HomePhoneNumber}"
                   Margin="0,5,5,5"/>
                                </Grid>
                            </Border>
                        </StackPanel>


                    </Border>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

这是我创建 iCommand 并将其绑定到按钮的代码:

  public ICommand cmdShow { get; set; }
        public ICommand cmddelete { get; set; }


        private FrameworkElement Window { get; set; }

        int index = 0;
public UserControl1()
        {
            InitializeComponent();
            InitalizeData();


            this.DataContext = this;

            Window = this;

            cmdShow = new RoutedCommand();
            cmddelete = new RoutedCommand();

            CommandManager.RegisterClassCommandBinding(Window.GetType(), new CommandBinding(cmdShow, cmdShow_Click));
            CommandManager.RegisterClassCommandBinding(Window.GetType(), new CommandBinding(cmddelete, cmddelete_Click));
        }

        protected void cmdShow_Click(object sender, ExecutedRoutedEventArgs e)
        {

            MessageBox.Show(lvDataBinding.SelectedIndex + "");

        }
        protected void cmddelete_Click(object sender, ExecutedRoutedEventArgs e)
        {

            MessageBox.Show("delete");
        }
        private void InitalizeData()
        {
            ObservableCollection<Patient> data = new ObservableCollection<Patient>();
            for (int i = 0; i < 3; i++)
            {
                data.Add(new Patient
                {
                    HomePhoneNumber = "0512-62810609"
                });
            }
            this.lvDataBinding.ItemsSource = data;
        }

        public class Patient
        {

            public string HomePhoneNumber { get; set; }

        }

根据您设置 DataContext 的位置,问题可能是由

引起的
AncestorType={x:Type **Window**}}