I have a ListBox
and items are generated dynamically. I also have a ComboBox
.
Each item has a string
property called 'Name'.
When I select this item on ListBox
, I want to set default value or text of ComboBox
with 'Name' of selected item from ListBox
.
I could bind this with 'Text' property of 'TextBlock'. But I couldn't do it with ComboBox
Here's my code:
XAML
<ListBox Name="MyList" ItemsSource="{Binding SourceList}"
SelectedItem="{Binding SelectedObject, Mode=TwoWay}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="{Binding ElementName=MyList, Path=ActualWidth}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border>
<StackPanel>
<Grid>
<StackPanel>
<TextBlock {Binding Path=FileName}" />
<TextBlock>
<Run Text="{Binding Name}" />
</TextBlock>
</StackPanel>
</Grid>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I could bind this TextBlock
:
<TextBlock>
<Run Text="{Binding SelectedItem.Name, ElementName=MyList}"/>
</TextBlock>
But it doesn't work with ComboBox
:
<ComboBox ItemsSource="{Binding ElementName=MyList, Path=SelectedItem}" SelectedItem="{Binding SelectedObject}" Text="{Binding Name}">
</ComboBox>