2012-12-24 65 views
4

我想將組合框顯示爲文本框(不帶邊框,背景,切換按鈕等) - 只顯示當前選定的項目文本。 我這樣做,但我不明白如何鏈接TextBlock,以便它顯示ComboBox中當前選定的項目。在WPF/Silverlight中呈現組合框爲TextBlock

<ComboBox ItemsSource="{Binding Path=...}" SelectedValue="{Binding Path=...}" DisplayMemberPath="Name" SelectedValuePath="Id"> 
    <ComboBox.Template> 
     <ControlTemplate> 
      <TextBlock Text="{Binding ?}"></TextBlock> 
     </ControlTemplate> 
    </ComboBox.Template> 
</ComboBox> 

回答

4
<ComboBox ItemsSource="{Binding Path=...}" SelectedValue="{Binding Path=...}" DisplayMemberPath="Name" SelectedValuePath="Id"> 
<ComboBox.Template> 
    <ControlTemplate> 
     <TextBlock Text="{Binding SelectedItem.MyText,RelativeSource={RelativeSource Mode=TemplatedParent}}"></TextBlock> 
    </ControlTemplate> 
</ComboBox.Template> 

0

你應該ControlTemplate指定TargetType並綁定到SelectionBoxItem
使用此:

<ComboBox> 
     <ComboBox.Template> 
      <ControlTemplate TargetType="{x:Type ComboBox}"> 
       <TextBlock Text="{TemplateBinding SelectionBoxItem}" /> 
      </ControlTemplate> 
     </ComboBox.Template> 
     <ComboBoxItem Content="Item1" IsSelected="True" /> 
     <ComboBoxItem Content="sdff" /> 
    </ComboBox> 
相關問題