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

时间:2013-09-02 09:28:02

标签: c# wpf

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

下拉单元格正确显示,但comboxbox文本块显示不同。

enter image description here

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

这是我的UserTemplate

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您可以设置ComboBox.DisplayMemberPath属性。

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