WPF Toolkit Combobox DisplayMemberPath无法正常工作

时间:2009-07-09 18:31:58

标签: wpf combobox toolkit

我正在使用非常方便的WPF Toolkit Xaml来拥有一个漂亮的GUI。我遇到了与组合框项目有关的问题。我已成功绑定到组合框控件,但下拉列表中的项目显示绑定对象,而不是显示成员。有趣的是,当我选择一个项目时,文本框中会显示正确的显示成员。

任何帮助将不胜感激!

标记。

我的XAML显示组合框:

<ComboBox Name="uxFeatureGroup" Margin="17,16,0,0" Height="23"
          HorizontalAlignment="Left"
          VerticalAlignment="Top" Width="162"
          DisplayMemberPath="FeatureGroupName"></ComboBox>

和WPF模板中的XAML:

<Style TargetType="{x:Type ComboBox}">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="FontFamily" Value="Trebuchet MS" />
    <Setter Property="FontSize" Value="12" />
    <Setter Property="Padding" Value="6,2,25,2" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="FocusedOn">
                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" />
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                    <Storyboard x:Key="FocusedOff">
                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" />
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </ControlTemplate.Resources>
                <Grid>
                    <ToggleButton Grid.Column="2" Template="{DynamicResource ComboBoxToggleButton}" x:Name="ToggleButton" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" />
                    <ContentPresenter HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="ContentSite" VerticalAlignment="Center" Content="{TemplateBinding ComboBox.SelectionBoxItem}" ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}" ContentStringFormat="{TemplateBinding ComboBox.SelectionBoxItemStringFormat}"  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" IsHitTestVisible="False" />

                    <TextBox Visibility="Hidden" Template="{DynamicResource ComboBoxTextBox}" HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="PART_EditableTextBox" Style="{x:Null}" VerticalAlignment="Center" Focusable="True" Background="Transparent" IsReadOnly="{TemplateBinding IsReadOnly}" />
                    <Rectangle x:Name="DisabledVisualElement" Fill="{DynamicResource DisabledBackgroundBrush}" Stroke="{DynamicResource DisabledBorderBrush}" RadiusX="0" RadiusY="0" IsHitTestVisible="false" Opacity="0" />
                    <Rectangle x:Name="FocusVisualElement" Margin="-1" Stroke="{DynamicResource FocusBrush}" StrokeThickness="1" IsHitTestVisible="false" Opacity="0" />
                    <Popup IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" PopupAnimation="Slide" Margin="0,1,0,0">
                        <Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True">
                            <Border x:Name="DropDownBorder" Margin="0,-1,0,0" BorderBrush="{DynamicResource ControlBorderBrush}" BorderThickness="1" CornerRadius="0,0,3,3" Background="{DynamicResource ControlBackgroundBrush}">
                                <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True">

                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />

                                </ScrollViewer>
                            </Border>
                        </Grid>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Trigger.ExitActions>
                            <BeginStoryboard Storyboard="{StaticResource FocusedOff}" x:Name="FocusedOff_BeginStoryboard" />
                        </Trigger.ExitActions>
                        <Trigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource FocusedOn}" />
                        </Trigger.EnterActions>
                    </Trigger>
                    <Trigger Property="HasItems" Value="false">
                        <Setter Property="MinHeight" Value="95" TargetName="DropDownBorder" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}" />
                        <Setter Property="Opacity" TargetName="DisabledVisualElement" Value="1" />
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                    </Trigger>
                    <Trigger Property="AllowsTransparency" SourceName="Popup" Value="true">
                        <Setter Property="CornerRadius" Value="4" TargetName="DropDownBorder" />
                        <Setter Property="Margin" Value="0,2,0,0" TargetName="DropDownBorder" />
                    </Trigger>
                    <Trigger Property="IsEditable" Value="true">
                        <Setter Property="IsTabStop" Value="false" />
                        <Setter Property="Visibility" Value="Visible" TargetName="PART_EditableTextBox" />
                        <Setter Property="Visibility" Value="Hidden" TargetName="ContentSite" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:1)

试试这个。

在您正在使用的主题的ComboBoxItem模板中,从ContentPresneter元素中删除Content和ContentTemplate属性。

所以,而不是:

<ContentPresenter x:Name="contentPresenter" 
                  Content="{TemplateBinding Content}" 
                  ContentTemplate="{TemplateBinding ContentTemplate}" 
                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                  Margin="{TemplateBinding Padding}" />

使用它:

<ContentPresenter x:Name="contentPresenter" 
                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                  Margin="{TemplateBinding Padding}" />