将文本框作为文本绑定到组合框

时间:2019-01-15 14:09:19

标签: wpf xaml binding

我试图将init$=new BehaviorSubject('initialvalue'); getData(init: boolean): Observable<any> { const refreshInterval = 5000; const interval = observableInterval(refreshInterval).pipe(startWith(0)) return interval.pipe( withLatestFrom(init$), flatMap(res=>this.apiSql.get<any>(this.getUrl(res[1]))), map((data) => this.doWhateverWithData(data)), ); } 所选项目绑定到init$.next('new value'),我已经尝试过:

ComboBox

和:

TextBox

但是当我选择项目时,我进入<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> <TextBox x:Name="freestyleSubtitleTxt" Text="{Binding ElementName=TitlesCombobox, Path=SelectedValue}" />

2 个答案:

答案 0 :(得分:2)

绑定到SelectedItem.Content

<TextBox x:Name="freestyleSubtitleTxt" 
         Text="{Binding ElementName=TitlesCombobox, Path=SelectedItem.Content}" />

或将ComboBoxItems替换为strings并绑定到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}" />

答案 1 :(得分:1)

要仅绑定到ComboBoxItem的值,请使用属性SelectedValuePath

您的ComboBox应该如下所示:

<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属性值的属性的路径。