Search code examples
c#wpfeventsdatatemplate

DataTemplate click event not work on UserControl


I created datatemplate for my listview with 2 button that must user click on if i use my datatemplate in Window everything's is OK and work fine but if I use my datatemplate in Usercontrol click event for my buttons not work so what is the problem? this is my codes:

<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>

And this is my code behind that I created iCommand and bind it to buttons :

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

        }

Solution

  • Depending where you set the DataContext the Problem is probably caused by

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