Reputation: 377
I am trying to bind the ComboBox
selected item to a TextBox
, I have tried:
<ComboBox x:Name="TitlesCombobox" IsEditable="True" IsReadOnly="True" Text="-- Subtitles --" >
<ComboBoxItem Content="sub title 1"/>
<ComboBoxItem Content="sub title 2"/>
<ComboBoxItem Content="sub title 3"/>
<ComboBoxItem Content="sub title 4"/>
</ComboBox>
and:
<TextBox x:Name="freestyleSubtitleTxt" Text="{Binding ElementName=TitlesCombobox, Path=SelectedValue}" />
but when I select item I get in TextBox
: System.Windows.Controls.ComboBoxItem: sub title 3
Upvotes: 1
Views: 96
Reputation: 279
To bind only to the value of a ComboBoxItem
use the Property SelectedValuePath
.
Your ComboBox
should look like this:
<ComboBox x:Name="TitlesCombobox"
IsEditable="True"
IsReadOnly="True"
Text="-- Subtitles --"
SelectedValuePath="Content">
<ComboBoxItem Content="sub title 1"/>
<ComboBoxItem Content="sub title 2"/>
<ComboBoxItem Content="sub title 3"/>
<ComboBoxItem Content="sub title 4"/>
</ComboBox>
The SelectedValuePath
property specifies the path to the property that is used to determine the value of the SelectedValue
property.
Upvotes: 1
Reputation: 169420
Bind to SelectedItem.Content
:
<TextBox x:Name="freestyleSubtitleTxt"
Text="{Binding ElementName=TitlesCombobox, Path=SelectedItem.Content}" />
Or replace the ComboBoxItems
with strings
and bind to SelectedItem
:
<ComboBox x:Name="TitlesCombobox" IsEditable="True" IsReadOnly="True" Text="-- Subtitles --"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<s:String>sub title 1</s:String>
<s:String>sub title 2</s:String>
<s:String>sub title 3</s:String>
<s:String>sub title 4</s:String>
</ComboBox>
<TextBox x:Name="freestyleSubtitleTxt" Text="{Binding ElementName=TitlesCombobox, Path=SelectedItem}" />
Upvotes: 2