1

I have a ListView in C# WPF which displays a ObservableCollection in a Master View and a Detail View. As default, In the Master View no item is selected. I want to have control, which item (index?) is selected in Master View, so that Detail View displays the content accordingly.

XAML Master View:

<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>
3
  • You can get by ListViewMaster.SelectedIndex. May 16, 2019 at 13:09
  • Mathivanan KP but how can I select a item from the list by using the index
    – Julian
    May 16, 2019 at 13:11
  • You realise you can just bind the datacontext of your detail view to the selecteditem of the listview? Or is your question more about how you set the selected item in code?
    – Andy
    May 16, 2019 at 15:18

2 Answers 2

2

Solution is simple:

  1. Create a new Bindable Property in your DataContext of the type that your ItemSource contains
  2. Add a SelectedItem="{Binding YourNewPropertyName}" to your ListView
  3. Be happy about having solved that problem for a brief moment

In case you need the index, not the actual item, solution is pretty much the same but with int property and SelectedIndex instead

Please note that you need to implement INotifyPropertyChanged properly to have this working. Example:

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

Your XAML then changes to this:

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

You can bind to a CollectionView instead of an ObservableCollection. The CollectionView has an event CurrentChanged where you can get the current selected item without binding to an extra property.

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.