选择一个列表视图

时间:2019-05-16 13:06:33

标签: c# wpf listview

我在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>

2 个答案:

答案 0 :(得分:2)

解决方案很简单:

  1. ItemSource所包含类型的DataContext中创建一个新的Bindable属性
  2. 向您的ListView添加SelectedItem="{Binding YourNewPropertyName}"
  3. 很高兴在短时间内解决了这个问题

如果您需要索引而不是实际项目,则解决方案几乎相同,但具有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;
}

您的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>

答案 1 :(得分:0)

您可以绑定到CollectionView而不是ObservableCollectionCollectionView有一个事件CurrentChanged,您可以在其中获得当前选定的项目,而不必绑定到额外的属性。

Viewmodel

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;
    }
}

XAML

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