Wpf DisplayMemberPath不适用于具有自定义ListBoxItem控件的ListBox

Wpf DisplayMemberPath不适用于具有自定义ListBoxItem控件的ListBox,wpf,Wpf,我已将ListBox配置为使用自定义的ControlTemplate,并将自定义的ControlTemplate配置为ListBoxItem。不幸的是,这样的更改禁用了DisplayMemberPath属性的默认值 // resource dictionary of ListBox control template <ControlTemplate x:Key="CustomListBox" TargetType="ListBox" xmlns="http://schemas.micro

我已将ListBox配置为使用自定义的
ControlTemplate
,并将自定义的ControlTemplate配置为
ListBoxItem
。不幸的是,这样的更改禁用了
DisplayMemberPath
属性的默认值

// resource dictionary of ListBox control template
<ControlTemplate  x:Key="CustomListBox" TargetType="ListBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib">
   <Border CornerRadius="3" BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="1,1,1,1" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="Bd" SnapsToDevicePixels="True">
       <ScrollViewer Padding="{TemplateBinding Control.Padding}" Focusable="False">
           <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"  />
       </ScrollViewer>
   </Border>
</ControlTemplate>
//trigger code omitted
<Style TargetType="ListBox">
    <Setter Property="Control.Template" Value="{StaticResource CustomListBox}"></Setter>
</Style>




// resource dictionary of ListBoxItem control template
    <ControlTemplate x:Key="CustomListBoxItem" TargetType="ListBoxItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel>
            <Border BorderThickness="0" CornerRadius="3" Padding="6 6" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="White" Name="Bd" SnapsToDevicePixels="True">
                <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
            </Border>
            <Label Height="3"></Label>

        </StackPanel>
 </ControlTemplate>
 // trigger code omitted
 <Style TargetType="ListBoxItem">
    <Setter Property="Control.Template" Value="{StaticResource CustomListBoxItem}"></Setter>
    <Setter Property="ItemsControl.DisplayMemberPath" Value="MatchText"></Setter>
</Style>
//列表框控件模板的资源字典
//触发代码省略
//ListBoxItem控件模板的资源字典
//触发代码省略
XAML



listBox
的数据上下文是使用代码设置的。如果我省略设置
CustomListBoxItem
作为控件模板,则它会按预期工作。当为
ListBox
ListBoxItem
使用自定义控件模板时,如何启用
displaymberpath

您的输出窗口中是否有任何错误?您看到了吗?显式设置模板时,DisplayMemberPath将不起作用。很好的发现。输出窗口中没有错误。谷歌搜索了4次,只发现了与组合框相关的问题。如果不使用自定义ControlTemplate,我认为这是可行的。如何准确地将DataTemplate导入ListBoxItem控件模板?或ListBox控件模板。不确定您究竟想要实现什么,但您可以在ControlTemplate中使用
而不是使用DisplayMemberPath。
<ListBox x:Name="listBox" DisplayMemberPath="MyCustomText">