C# WPF组合框:文本框和下拉列表中的不同结果

C# WPF组合框:文本框和下拉列表中的不同结果,c#,wpf,C#,Wpf,我有一个将数据绑定到viewmodel的combox 下拉单元格显示正确,但comboxbox文本块不显示相同的内容 这是我的组合框xaml <ComboBox x:Name="UserComboBox" ItemsSource="{Binding userInfoViewModel.Users}" SelectedItem="{Binding userInfoViewModel.SelectedUser, Mode=TwoWay}" ItemT

我有一个将数据绑定到viewmodel的combox

下拉单元格显示正确,但comboxbox文本块不显示相同的内容

这是我的组合框xaml

<ComboBox x:Name="UserComboBox" ItemsSource="{Binding userInfoViewModel.Users}" 
       SelectedItem="{Binding userInfoViewModel.SelectedUser, Mode=TwoWay}"  
           ItemTemplate="{StaticResource UserTemplate}"
           ItemContainerStyle="{DynamicResource CustomUserComboBoxStyle}" 
           HorizontalAlignment="Left" Margin="39.025,217.76,0,186.025" Width="204.975" IsEditable="True" Background="#FFF3F3F3" 
           BorderBrush="{DynamicResource CustomBorder}" 
           Height="27.24" >
</ComboBox>

这是我的用户模板

<DataTemplate x:Key="UserTemplate">
      <StackPanel FlowDirection="LeftToRight"  Orientation="Horizontal">
          <TextBlock Text="{Binding Path=InternalNumber}"></TextBlock>
          <TextBlock Text="{Binding Path=UserName}" ></TextBlock>
      </StackPanel>
</DataTemplate>

还有我的CustomUserComboxStyle

<Style x:Key="CustomUserComboBoxStyle" TargetType="{x:Type ComboBoxItem}">
        <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="Padding" Value="3,0,3,0"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
                            Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsHighlighted" Value="true">
                            <Setter Property="Background" Value="#646363" />
                            <Setter Property="Foreground" Value="#FFFFFFFF"/>
                            <Setter Property="BorderBrush" Value="#646363"/>
                        </Trigger>
                        <Trigger Property="IsHighlighted" Value="false">
                            <Setter Property="Background" Value="#FFF3F3F3" />
                            <Setter Property="BorderBrush" Value="#FFF3F3F3"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


为什么组合框显示Digicom.Eventserver.Client.user而不是名称和编号

最简单的解决方案是重写用户数据类中的
ToString()
方法:

public override string ToString()
{
    return string.Concat(InternalNumber, UserName);
}

您可以设置
组合框.DisplayMemberPath
属性


该属性只接受DataContext对象的一个属性的名称。如果您想使用两个属性(InternalNumber和Username),您应该使用ValueConverter或(更好的方式是IMHO)ViewModel类,这样您就不必使用模板了。