DataTemplate单​​击事件不适用于UserControl

时间:2018-03-31 19:53:31

标签: c# wpf events datatemplate

我为我的listview创建了datatemplate 2个按钮必须用户点击如果我在Window中使用我的datatemplate一切正常并且工作正常但是如果我在Usercontrol中使用我的datatemplate我的按钮单击事件不起作用问题是什么?这是我的代码:

<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\png\32x32\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\png\32x32\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; }

        }

1 个答案:

答案 0 :(得分:0)

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

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