Xaml 如何将命令添加到ListBox.ItemTemplate
我有一个使用MVVM的WPF应用程序 此代码来自my ResourceDictionary.xamlXaml 如何将命令添加到ListBox.ItemTemplate,xaml,binding,command,Xaml,Binding,Command,我有一个使用MVVM的WPF应用程序 此代码来自my ResourceDictionary.xaml <DataTemplate x:Key="LogListTemplate"> <ListBox ItemsSource="{Binding LogList, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource HorizontalListBoxItem}">
<DataTemplate x:Key="LogListTemplate">
<ListBox ItemsSource="{Binding LogList, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource HorizontalListBoxItem}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Style="{StaticResource ErrorBorders}">
<StackPanel ToolTip="{Binding Details}" Width="250">
<TextBlock Text="{Binding Title}" />
<TextBlock Text="{Binding Details}" TextWrapping="Wrap" />
<Button Command="{Binding AddToTempDtdFileCommand}" CommandParameter="{Binding Details}" Content="Ignore" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
有人有什么建议吗?如果该命令是在
ViewModel
中定义的,您可以使用RelativeSource
访问它RelativeSource
可以找到祖先(这是您的视图
),因此您可以使用它的数据上下文
(您的视图模型
)
1. Asthetics/UI (the button has belong with the information)
2. Binding to the LogList is done from within the ListBox.ItemTemplate
<Button Command="{Binding DataContext.AddToTempDtdFileCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourViewClass}}}" CommandParameter="{Binding Details}" Content="Ignore" />