WPF Databinding ListView with ComboBox Selected Item Issue
I have a databound ComboBox where I cannot seem to set the SelectedItem. I have tried SelectedValue/SelectedValuePath, but have having difficulties.
To explain the scenario, I have a parent ListView which contains ComboBoxes w/in the ListViewItems. The parent ListView and the child ComboBox have the same datasource, but display different data. For example, Extension 2 references Extension 1. In this case I am trying to illustrate that 2 mirrors one. The user needs to be able to change which Extension it points to, itself or any of the others. Other than that it is very simple, but almost have it.
Here the example which you can run from your favorite xaml editor.
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="xmlDataProvider">
<x:XData>
<Extensions xmlns="">
<Extension>
<ExtId>1</ExtId>
<ExtName>Extension 1</ExtName>
<ExtValue>1</ExtValue>
</Extension>
<Extension>
<ExtId>2</ExtId>
<ExtName>Extension 2</ExtName>
<ExtValue>1</ExtValue>
</Extension>
<Extension>
<ExtId>3</ExtId>
<ExtName>Extension 3</ExtName>
<ExtValue>3</ExtValue>
</Extension>
<Extension>
<ExtId>4</ExtId>
<ExtName>Extension 4</ExtName>
<ExtValue>4</ExtValue>
</Extension>
</Extensions>
</x:XData>
</XmlDataProvider>
<!-- Extensions -->
<CollectionViewSource
x:Key="CollectionViewSourceExtensions"
Source="{Binding Source={StaticResource xmlDataProvider}, XPath=Extensions/Extension}" />
</Page.Reso开发者_开发百科urces>
<Grid>
<ListView
ItemsSource="{Binding
Source={StaticResource CollectionViewSourceExtensions},
Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding XPath=ExtName}" />
<TextBlock Text=" - " />
<TextBlock Text="{Binding XPath=ExtValue}" />
</StackPanel>
<ComboBox
SelectedItem="{Binding XPath=ExtId}"
ItemsSource="{Binding
Source={StaticResource CollectionViewSourceExtensions},
Mode=OneTime}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=ExtId}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
Was a simple resolution that took a good deal of time to finally figure out...
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="SamplePeople">
<x:XData>
<Extensions xmlns="">
<Exension>
<Id>1</Id>
<Name>Line Key 1</Name>
<Value>1</Value>
</Exension>
<Exension>
<Id>2</Id>
<Name>Line Key 2</Name>
<Value>1</Value>
</Exension>
<Exension>
<Id>3</Id>
<Name>Line Key 3</Name>
<Value>3</Value>
</Exension>
<Exension>
<Id>4</Id>
<Name>Line Key 4</Name>
<Value>4</Value>
</Exension>
</Extensions>
</x:XData>
</XmlDataProvider>
</Page.Resources>
<Grid>
<ListBox x:Name="PeopleListBox"
DataContext="{Binding Source={StaticResource SamplePeople}}"
ItemsSource="{Binding Mode=Default, XPath=/Extensions/node()}"
IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding XPath=Name}" />
<TextBlock Text=" is " />
<ComboBox
SelectedValue="{Binding XPath=Value}"
ItemsSource="{Binding Mode=Default, XPath=/Extensions/Exension/Id}" >
</ComboBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Page>
精彩评论