我有两个ListView,每个列表包含一些行.我想在行选择后调用函数.但我有一个问题,当选择行或点击此行中的按钮时触发事件"GotFocus".当我使用<i:EventTrigger EventName="Selected">它时,当选择表中的行时不会触发.我需要做什么?
XAML:
<Grid>
<ListView Width="200" Height="200" ItemsSource="{Binding Items}" HorizontalAlignment="Left">
<ListView.ItemTemplate>
<DataTemplate>
<Button Content="{Binding .}">
</Button>
</DataTemplate>
</ListView.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<i:InvokeCommandAction Command="{Binding DataContext.TestCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type vm:MainWindow }}}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
<ListBox Width="200" Height="200" ItemsSource="{Binding Items}" HorizontalAlignment="Right">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding .}">
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<i:InvokeCommandAction Command="{Binding DataContext.TestTestCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type vm:MainWindow }}}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
码:
namespace WpfApplication129
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = new Data();
InitializeComponent();
}
}
public class Data
{
public ICommand TestCommand { get; set; }
public ICommand TestTestCommand { get; set; }
public List<string> Items { get; set; }
public Data()
{
TestCommand = new RelayCommand(() => Test());
TestTestCommand = new RelayCommand(() => TestTest());
Items = new List<string>();
Items.Add("first");
Items.Add("Second");
Items.Add("Third");
}
public void Test()
{
MessageBox.Show("Running");
}
public void TestTest()
{
MessageBox.Show("TestRunning");
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8292 次 |
| 最近记录: |