2013-04-24 15 views
0

所以我有兩個列表視圖是由第一列(TestID)相關。我想要在第二列表視圖中自動選擇行,當我選擇第一列表視圖中的行時。這是我迄今爲止。我想在第二個listview中選擇同一行後,我在wpf的第一個listview中選擇它,那該怎麼做?

AutomationTestResults tr = new AutomationTestResults(); 
    public int SelectedTestID 
    { 

     get 
     { 

      return tr.TestID; 
     } 

     set 
     { 
      tr.TestID = value; 
      NotifyPropertyChanged("SelectedTestID"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void NotifyPropertyChanged(String propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

============= XAML ============================= ===========================

<ListView ItemsSource="{Binding TRCollection }" SelectedItem="{Binding SelectedTestID}" SelectionChanged="ListView_SelectionChanged" 
        Grid.Row="1" Height="197" HorizontalAlignment="Left" Margin="12,22,0,0" Name="listView1" VerticalAlignment="Top" Width="680"> 
     </ListView> 
     <ListView ItemsSource="{Binding TCCollection}" SelectedItem="{Binding SelectedTestID}" SelectionChanged="ListView_SelectionChanged" 
        Grid.Row="1" Height="245" HorizontalAlignment="Left" Margin="12,251,0,0" Name="listView2" VerticalAlignment="Top" Width="680" > 

回答

1

如果所有你需要做的是在第二個ListView中選擇相應的行,你可以像這樣綁定「SelectedIndex」屬性

<ListView ItemsSource="{Binding TRCollection }" SelectedItem="{Binding SelectedTestID}" SelectionChanged="ListView_SelectionChanged" 
      Grid.Row="1" Height="197" HorizontalAlignment="Left" Margin="12,22,0,0" Name="listView1" VerticalAlignment="Top" Width="680"/> 

<ListView ItemsSource="{Binding TCCollection}" SelectedIndex="{Binding Path=SelectedIndex, ElementName=listView1}" SelectedItem="{Binding SelectedTestID}" SelectionChanged="ListView_SelectionChanged" 
      Grid.Row="1" Height="245" HorizontalAlignment="Left" Margin="12,251,0,0" Name="listView2" VerticalAlignment="Top" Width="680" /> 
0

爲什麼不

<ListView x:Name="listView1"/> <!-- etc.. --> 

<ListView SelectedItem="{Binding SelectedItem, ElementName=listView1}"/> 
+0

我ListView1的是這樣的,當listview2行正在運行它只會得到填充... TestID列1 1 ASD 2 ASD 3 ASD 我的第二個listview2看起來像這樣 TestID COLUMN1運行 1 BSE 1 2 bse 1 3 bse 1 如果我使用選定的索引,那麼我可以在列表視圖2中運行行'3',它將在列表視圖1中顯示爲行'1'。所以這就是爲什麼它更好地與TestID號相關聯......但我不知道如何做到這一點:S – Froodo 2013-04-24 18:34:23

+0

@ user2316777我不知道你在說什麼。我正在使用'SelectedItem'屬性,而不是'SelectedIndex'。這應該工作,如果項目是相同的(這意味着'Object.Equals()== true'。) – 2013-04-24 18:36:43

相關問題