C# 选择ListView的一项

C# 选择ListView的一项,c#,wpf,listview,C#,Wpf,Listview,我在C#WPF中有一个ListView,它在主视图和详细视图中显示一个ObservableCollection。默认情况下,在主视图中未选择任何项目。我想要控制,在主视图中选择哪个项目(索引?),以便详细视图相应地显示内容 XAML主视图: <ListView x:Name="ListViewMaster" Height="100" Width="130" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Bi

我在C#WPF中有一个ListView,它在主视图和详细视图中显示一个ObservableCollection。默认情况下,在主视图中未选择任何项目。我想要控制,在主视图中选择哪个项目(索引?),以便详细视图相应地显示内容

XAML主视图:

<ListView x:Name="ListViewMaster" Height="100" Width="130" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding ListResult}" IsSynchronizedWithCurrentItem="True" SelectionChanged="ListViewMaster_SelectionChanged">
    <ListView.View>
        <GridView AllowsColumnReorder="False"  >
            <GridViewColumn Header="Head" Width="130">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding myID}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

解决方案很简单:

  • 在您的
    ItemSource
    包含的类型的DataContext中创建一个新的可绑定属性
  • SelectedItem=“{Binding YourNewPropertyName}”
    添加到ListView
  • 很高兴能暂时解决这个问题
  • 如果您需要的是索引,而不是实际项目,那么解决方案基本相同,但使用
    int
    属性,而不是
    SelectedIndex

    请注意,您需要正确实施
    INotifyPropertyChanged
    ,才能使其正常工作。例如:

    public class MyDataContext : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        public ObservableCollection<MyItem> ListResult { get; }
    
        public MyItem MySelectedItem
        {
            get => this._MySelectedItem;
            set
            {
                this._MySelectedItem = value;
                this.PropertyChanged?.Invoke(this,
                    new PropertyChangedEventArgs(nameof(this.MySelectedItem)));
            }
        }
        private MyItem _MySelectedItem;
    
        public int MySelectedIndex
        {
            get => this._MySelectedIndex;
            set
            {
                this._MySelectedIndex = value;
                this.PropertyChanged?.Invoke(this,
                    new PropertyChangedEventArgs(nameof(this.MySelectedIndex)));
            }
        }
        private int _MySelectedIndex;
    }
    
    公共类MyDataContext:INotifyPropertyChanged
    {
    公共事件属性更改事件处理程序属性更改;
    公共ObservableCollection ListResult{get;}
    公共MyItem MySelectedItem
    {
    get=>这个;
    设置
    {
    这是._MySelectedItem=值;
    this.PropertyChanged?调用(this,
    新属性changedeventargs(name of(this.MySelectedItem));
    }
    }
    私有MyItem _MySelectedItem;
    公共整数MySelectedIndex
    {
    get=>这个;
    设置
    {
    这个.\u MySelectedIndex=值;
    this.PropertyChanged?调用(this,
    新属性ChangedEventArgs(name of(this.MySelectedIndex));
    }
    }
    私人int_MySelectedIndex;
    }
    
    然后,您的XAML将更改为:

    <ListView x:Name="ListViewMaster" Height="100" Width="130"
              HorizontalAlignment="Left" VerticalAlignment="Top"
              ItemsSource="{Binding ListResult}"
              IsSynchronizedWithCurrentItem="True"
              SelectedItem="{Binding MySelectedItem}"
              SelectedIndex="{Binding MySelectedIndex}">
        <ListView.View>
            <GridView AllowsColumnReorder="False"  >
                <GridViewColumn Header="Head" Width="130">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding myID}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
    

    您可以绑定到
    集合视图,而不是
    可观察集合
    CollectionView
    有一个事件
    CurrentChanged
    ,您可以在该事件中获取当前选定项,而无需绑定到其他属性

    视图模型

    public class MyViewModel : INotifyPropertyChanged
    {
        //TODO implement INotifyPropertyChanged
    
        public MyViewModel()
        {
            Init();
        }
    
        private ICollectionView _listResult;
        public ICollectionView ListResult
        {
            get => _listResult;
            set
            {
                if (Equals(value, _listResult)) return;
                _listResult = value;
                OnPropertyChanged();
            }
        }
    
        private void Init(){            
            // you can use an ObservableCollection instead of a List if you need
            // the update functionality
            var results = new List<ResultVm>(){....};
    
            ListResult = new CollectionView(results);
    
            // Event when another item gets selected
            ListResult.CurrentChanged += ListResultOnCurrentChanged;
    
            // moves selected index to postion 2
            ListResult.MoveCurrentToPosition(2); 
        }
    
        private void ListResultOnCurrentChanged(object sender, EventArgs e)
        {
            // the currently selected item
            ResultVm resultVm = (ResultVm)ListResult.CurrentItem;
    
            // the currently selected index
            int currentIndex = ListResult.CurrentPosition;
        }
    }
    
    公共类MyViewModel:INotifyPropertyChanged
    {
    //TODO实现INotifyProperty已更改
    公共MyViewModel()
    {
    Init();
    }
    私有ICollectionView_listResult;
    公共ICollectionView列表结果
    {
    获取=>\u列表结果;
    设置
    {
    如果(等于(值,_listResult))返回;
    _listResult=值;
    OnPropertyChanged();
    }
    }
    私有void Init(){
    //如果需要,可以使用ObservableCollection而不是列表
    //更新功能
    var results=newlist(){….};
    ListResult=新的CollectionView(结果);
    //选择另一项时发生的事件
    ListResult.CurrentChanged+=ListResultOnCurrentChanged;
    //将所选索引移动到位置2
    ListResult.MoveCurrentToPosition(2);
    }
    私有无效列表ResultOnCurrentChanged(对象发送方,事件参数e)
    {
    //当前选定的项目
    ResultVm ResultVm=(ResultVm)ListResult.CurrentItem;
    //当前选定的索引
    int currentIndex=ListResult.CurrentPosition;
    }
    }
    
    XAML

    
    
    您可以通过
    ListViewMaster.SelectedIndex
    .Mathivanan KP获取,但是如何使用索引从列表中选择项目?您知道,您可以将详细视图的数据上下文绑定到listview的selecteditem?或者您的问题更多的是关于如何在代码中设置所选项的?
    <ListView 
        x:Name="ListViewMaster" 
        Height="100" 
        Width="130"
        HorizontalAlignment="Left" 
        VerticalAlignment="Top"
        ItemsSource="{Binding ListResult}" 
        IsSynchronizedWithCurrentItem="True">
        <ListView.View>
            <GridView AllowsColumnReorder="False"  >
                <GridViewColumn Header="Head" Width="130">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding myID}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>