0

我有一个组合框,我将数据绑定到视图模型。

下拉单元格显示正确,但组合框文本块显示不一样。

在此处输入图像描述

这是我的组合框 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>

和我的 CustomUserComboBoxStyle

<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 而不是名称和编号?

4

2 回答 2

1

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

public override string ToString()
{
    return string.Concat(InternalNumber, UserName);
}
于 2013-09-02T09:31:28.917 回答
0

你可以设置ComboBox.DisplayMemberPath属性。

该属性只接受 DataContext 对象的一个​​属性的名称。如果您想使用两个属性(InternalNumber 和 Username),您应该使用 ValueConverter 或(恕我直言更好的方式)ViewModel 类

于 2013-09-02T09:38:56.483 回答