.net WPF ComboBoxItem数据模板可以';t单击要选择的项目文本

.net WPF ComboBoxItem数据模板可以';t单击要选择的项目文本,.net,wpf,.net,Wpf,我正在用项目资源填充组合框,并显示一个简单的绑定: <ComboBox ItemsSource="{Binding Locations}" SelectedItem="{Binding Location}" > <ComboBox.ItemTemplate> <DataTemplate> <ComboBoxItem Content="{Binding Name}" />

我正在用
项目资源填充
组合框
,并显示一个简单的
绑定

<ComboBox 
    ItemsSource="{Binding Locations}" 
    SelectedItem="{Binding Location}"
>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <ComboBoxItem Content="{Binding Name}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

我无法通过单击文本来选择新的组合框项目(我必须在文本之外单击):


因此,如果我单击内边框(文本容器)内的深蓝色区域,它不会更新选择。如果我单击浅蓝色区域,它会按预期更新。为什么会发生这种情况?

ItemTemplate中不应该有ComboBoxItem(因为它被用作另一个自动生成的ComboxItem的ContentTemplate)

改为使用文本块:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Name}"/>
    </DataTemplate>
</ComboBox.ItemTemplate>

或者只需删除整个ItemTemplate并设置DisplayMemberPath:

<ComboBox 
    DisplayMemberPath="Name"
    ItemsSource="{Binding Locations}" 
    SelectedItem="{Binding Location}"/>

ItemTemplate中不应该有ComboBoxItem(因为它被用作另一个自动生成的ComboxItem的ContentTemplate)

改为使用文本块:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Name}"/>
    </DataTemplate>
</ComboBox.ItemTemplate>

或者只需删除整个ItemTemplate并设置DisplayMemberPath:

<ComboBox 
    DisplayMemberPath="Name"
    ItemsSource="{Binding Locations}" 
    SelectedItem="{Binding Location}"/>


Aha真管用!非常感谢。我将使用
,因为我在那里有一些触发器。啊哈,这很有效!非常感谢。我将使用
,因为我在那里有一些触发器。