Wpf 将textbox作为文本绑定到combobox
我正在尝试将Wpf 将textbox作为文本绑定到combobox,wpf,xaml,binding,Wpf,Xaml,Binding,我正在尝试将组合框所选项目绑定到文本框,我已尝试: <ComboBox x:Name="TitlesCombobox" IsEditable="True" IsReadOnly="True" Text="-- Subtitles --" > <ComboBoxItem Content="sub title 1"/> <ComboBoxItem Content="sub title 2"/> <ComboBoxItem Co
组合框
所选项目绑定到文本框
,我已尝试:
<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>
以及:
但当我选择项目时,我会进入
文本框
:System.Windows.Controls.ComboBoxItem:子标题3
绑定到SelectedItem.Content
:
<TextBox x:Name="freestyleSubtitleTxt"
Text="{Binding ElementName=TitlesCombobox, Path=SelectedItem.Content}" />
要仅绑定到
ComboBoxItem
的值,请使用属性SelectedValuePath
您的组合框应如下所示:
<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>
SelectedValuePath
属性指定用于确定SelectedValue
属性值的属性路径
<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}" />
<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>