Introduce ComboBox as TextBlock in WPF / Silverlight

I want to display the ComboBox as a TextBox (without frame, background, toggle button, ect.) - only the current selected text of the element. I like it, but I don’t understand how to link the TextBlock so that it displays the currently selected item in the ComboBox.

<ComboBox ItemsSource="{Binding Path=...}" SelectedValue="{Binding Path=...}" DisplayMemberPath="Name" SelectedValuePath="Id"> <ComboBox.Template> <ControlTemplate> <TextBlock Text="{Binding ?}"></TextBlock> </ControlTemplate> </ComboBox.Template> </ComboBox> 
+4
source share
2 answers
 <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> 

+4
source

You must specify the TargetType in the ControlTemplate and bind to the SelectionBoxItem
Use this:

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

All Articles