按钮事件不是从列表框项目模板wpf触发的

时间:2017-07-23 01:08:09

标签: wpf listbox buttonclick itemtemplate

这是我的xaml

           <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Blue"
                            BorderThickness="1"
                            HorizontalAlignment="Stretch">
                        <StackPanel HorizontalAlignment="Stretch"
                                    Orientation="Horizontal">
                            <TextBlock Margin="5"
                                       Text="{Binding Text}" />
                            <Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
                                    Click="Remove">
                                <Button.Content>
                                    <Image Source="{Binding DeleteIcon}"
                                           Stretch="Fill"
                                           Height="15"
                                           Width="20" />
                                </Button.Content>
                            </Button>
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>

按钮事件不会在后面的代码中触发,也不会在带有命令绑定的viewmodel中触发。如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试这个......它为我工作......

<ListBox Name="lstNumbers">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Blue"
                            BorderThickness="1"
                            HorizontalAlignment="Stretch">
                        <StackPanel HorizontalAlignment="Stretch"
                                    Orientation="Horizontal">
                            <TextBlock Margin="5"
                                       Text="{Binding Text}" />
                            <Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
                                    Click="Remove">
                                <Button.Content>
                                    <Image Source="{Binding DeleteIcon}"
                                           Stretch="Fill"
                                           Height="15"
                                           Width="20" />
                                </Button.Content>
                            </Button>
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

<强> Cs Code:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<Numbers> list = new List<Numbers>();
            list.Add(new Numbers() { Text ="1"});
            list.Add(new Numbers() { Text = "2" });
            lstNumbers.ItemsSource = list;
        }

        private void Remove(object sender, RoutedEventArgs e)
        {

        }

        public class Numbers
        {
            public string Text { get; set; }
        }