Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按钮事件不会从列表框项目模板wpf触发_Wpf_Listbox_Buttonclick_Itemtemplate - Fatal编程技术网

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

按钮事件不会从列表框项目模板wpf触发,wpf,listbox,buttonclick,itemtemplate,Wpf,Listbox,Buttonclick,Itemtemplate,这是我的xaml <ListBox.ItemTemplate> <DataTemplate> <Border BorderBrush="Blue" BorderThickness="1" HorizontalAlignment="Stretch">

这是我的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中不会触发按钮事件。如何修复此问题?

尝试此。。。这是我的工作

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

你做了什么改变?在按钮中,单击我想要获取所选列表项tooXaml代码:。Cs代码:private void Remove(object sender,RoutedEventArgs e){var selectedItem=((按钮)sender).Tag;}@ganit没有为我触发单击事件,此列表框位于一个用户控件中,它是另一个用户控件的子控件,您认为这里有什么问题吗?这是获取System.Windows.Data时出现的错误错误错误:40:BindingExpression路径错误:“在对象”“学生”(HashCode=11949699)“上找不到DeleteIcon”属性。BindingExpression:Path=DataContext.DeleteIcon;DataItem='ListBoxItem'(名称='');目标元素是“按钮”(名称=“”);目标属性为“Command”(类型为“ICommand”)
 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; }
        }