5

我想将 ComboBox 显示为 TextBox (没有边框、背景、切换按钮等) - 只有当前选定的项目文本。我确实喜欢这样,但我不明白如何链接 TextBlock,以便它在 ComboBox 中显示当前选定的项目。

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

2 回答 2

5
<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>

于 2012-12-24T18:22:08.613 回答
0

您应该指定TargetTypeinControlTemplate并绑定到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>
于 2012-12-24T18:12:51.163 回答