这是我的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中触发。如何解决这个问题?
答案 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; }
}