2015-08-19 19 views
5

Ho due ListView, ogni lista contiene alcune righe. Voglio chiamare la funzione dopo la selezione della riga. Ma ho un problema, l'evento "GotFocus" si attiva quando viene selezionata la riga selezionata o il pulsante in questa riga. Quando uso <i:EventTrigger EventName="Selected"> non si attiva quando viene selezionata una riga nella tabella. Cosa devo fare?Wpf listview evento di selezione articolo

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> 

Codice:

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"); 
    } 
} 
} 
+0

Che cos'è l'evento "Selezionato"? Dove lo trovi/lo usi? – Sinatr

+0

A191919

risposta

Problemi correlati