【问题标题】:I want to select the same row on the 2nd listview after i select it on the 1st listview in wpf, how can that be done?我想在 wpf 的第一个 listview 上选择它之后在第二个 listview 上选择同一行,怎么做?
【发布时间】:2013-04-18 09:56:21
【问题描述】:

所以我有两个列表视图,它们与第一列 (TestID) 相关。当我在第一个列表视图中选择行时,我希望在第二个列表视图中自动选择该行。这是我目前所拥有的。

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" >

【问题讨论】:

    标签: wpf xaml events listview binding


    【解决方案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" />
    

    【讨论】:

      【解决方案2】:

      为什么不直接

      <ListView x:Name="listView1"/> <!-- etc.. -->
      
      <ListView SelectedItem="{Binding SelectedItem, ElementName=listView1}"/> 
      

      【讨论】:

      • 我的 listView1 看起来像这样,它只会在 listview2 行运行时填充... TestID Column1 1 asd 2 asd 3 asd 我的第二个 listview2 看起来像这样 TestID column1 Run 1 bse 1 2 bse 1 3 bse 1 如果我使用选定的索引,那么我可以从 listview2 运行第 3 行,它将在 listview1 中显示为第 1 行。所以这就是为什么最好将它与 TestID 号码相关联......但我不知道该怎么做:S
      • @user2316777 我不知道你在说什么。我使用的是SelectedItem 属性,而不是SelectedIndex。如果项目相同(这意味着Object.Equals() == true),这应该可以工作。
      猜你喜欢
      • 2012-01-20
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多