0

I have an observable collection with KeyValuePair. I need to get the key of the selected item.

 private ObservableCollection<KeyValuePair<String, Items>> _items;


 <ListView Grid.Column="0" HorizontalAlignment="Stretch" Padding="5" ItemsSource="{Binding Items}" SelectedValue="{Binding SelectedThings, Mode=TwoWay}"  SelectedValuePath="Value" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                            <Image Height="40" Source="Resources/Apps64.png"/>
                            <TextBlock MinWidth="120" HorizontalAlignment="Stretch" TextAlignment="Center" Text="{Binding Key, UpdateSourceTrigger=PropertyChanged}" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
2
  • can't remember out the top of my head, but try using WPF inspector. It will help you hunt down solutions like this.
    – user853710
    Jan 25, 2016 at 9:12
  • Do you want the key to be available in the viewmodel or in the view?
    – Zippy
    Jan 25, 2016 at 9:14

1 Answer 1

1

Assuming that your Items collections is something like this :

private ObservableCollection<KeyValuePair<string,string>> _items = new ObservableCollection<KeyValuePair<string, string>>();
    public ObservableCollection<KeyValuePair<string,string>> Items
    {
        get
        {
            return _items;
        }

        set
        {
            if (_items == value)
            {
                return;
            }

            _items = value;                
        }
    }

then to get the SelectedItem Key from your viewModel, simply create a KeyValuePaire property in your view model and bind it to your ListView's SelectedItem

private KeyValuePair<string,string> _selectedItem  ;
    public KeyValuePair<string,string> SelectedItem
    {
        get
        {
            return _selectedItem;
        }

        set
        {
            if (_selectedItem.Equals(value))
            {
                return;
            }

            _selectedItem = value; 

        }
    }

and to get the Key of the selectedItem in your Xaml simply use ElementName Binding to the ListView's SelectedItem :

 <ListView x:Name="Lv" Grid.Column="0" HorizontalAlignment="Stretch" Padding="5" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" SelectedValue="{Binding SelectedThings, Mode=TwoWay}"  SelectedValuePath="Value" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                    <Image Height="40" Source="Resources/Apps64.png"/>
                    <TextBlock MinWidth="120" HorizontalAlignment="Stretch" TextAlignment="Center" Text="{Binding ElementName=Lv,Path=SelectedItem.Key}" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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