DisplayMemberPath的ComboBox样式问题

时间:2009-04-24 10:43:11

标签: wpf templates combobox styles selecteditem

我有一个ComboBox,我已将 combo.ItemsSource 属性设置为 List 对象。 Book 类包含两个属性:“缩写”“名称”。

我已将ComboBox的 DisplayMemberPath 设置为“缩写”,但在ComboBox上设置的以下样式不显示缩写属性,而是显示“ Words.Book “这是该类的名称。 ComboBox下拉列表正确显示列表(使用指定的缩写属性)。问题出在选定的ComboBox项中,即ComboBox'下拉列表关闭时显示的项目。

我想问题出现在DataTemplate的ContentPresenter中,但我无法找到成功的解决方案。目前,ContentPresenter的内容属性设置为Content =“{TemplateBinding Content}但我不知道它是否应该如何。

如何在所选项目上显示DisplayMemberPath中指定的属性?

谢谢

代码:

<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="20" />
        </Grid.ColumnDefinitions>
        <Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="2" BorderThickness="1" Background="{DynamicResource ribbonTitleFade}" />
        <Border Grid.Column="0" CornerRadius="2,0,0,2" Margin="1,6,1,6" BorderBrush="{DynamicResource boSecE}" BorderThickness="0,0,1,0" />
        <Path x:Name="Arrow" Grid.Column="1" Fill="White" 
              HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"
              />
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="ToggleButton.IsMouseOver" Value="true">
            <Setter TargetName="Border" Property="Background" Value="{DynamicResource ribbonTitleFade}" />
        </Trigger>
        <Trigger Property="ToggleButton.IsChecked" Value="true">
            <Setter TargetName="Border" Property="Background" Value="{DynamicResource ribbonTitleFade}" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="Border" Property="Background" Value="Black" />
            <Setter TargetName="Border" Property="BorderBrush" Value="Black" />
            <Setter Property="Foreground" Value="Gray"/>
            <Setter TargetName="Arrow" Property="Fill" Value="Gray" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<Style x:Key="comboBoxBorderTransparent" TargetType="Control">
    <Setter Property="BorderBrush" Value="{DynamicResource boPrimC}" />
</Style>


<Style x:Key="comboItemStyle" TargetType="ComboBoxItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                <Border x:Name="backBorder" >
                    <StackPanel Orientation="Horizontal">
                        <Rectangle x:Name="rectA" Stroke="White" Width="4" Height="4" Fill="#80FFFFFF" Margin="5,0,0,0" HorizontalAlignment="Left" />
                        <TextBlock x:Name="text" Foreground="White" FontSize="10px">
                            <ContentPresenter Margin="8,1,0,1"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              />
                        </TextBlock>
                    </StackPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="ComboBoxItem.IsMouseOver" Value="true">
                        <Setter TargetName="rectA" Property="Stroke" Value="Black" />
                        <Setter TargetName="rectA" Property="Fill" Value="#80000000" />
                        <Setter TargetName="backBorder" Property="Background" Value="White"/>
                        <Setter TargetName="text" Property="Foreground" Value="{DynamicResource boPrimC}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<DataTemplate x:Key="comboSelectedItemTemplate">
    <TextBlock Foreground="White" FontSize="10px">
        <ContentPresenter Content="{TemplateBinding Content}" />
    </TextBlock>
</DataTemplate>

<Style TargetType="ComboBox">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="MinWidth" Value="60"/>
    <Setter Property="MinHeight" Value="20"/>
    <Setter Property="ItemContainerStyle" Value="{DynamicResource comboItemStyle}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBox">
                <Grid>
                    <ToggleButton 
                        Name="ToggleButton" Grid.Column="2" 
                        Template="{StaticResource ComboBoxToggleButton}" 
                        Focusable="false"
                        IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                        ClickMode="Press">
                    </ToggleButton>

                    <ContentPresenter HorizontalAlignment="Center"
                                      VerticalAlignment="Center"
                                      Name="ContentSite"
                                      IsHitTestVisible="False" 
                                      Content="{TemplateBinding SelectionBoxItem}"
                                      ContentTemplate="{DynamicResource comboSelectedItemTemplate}"
                                      ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                      Margin="3,3,23,3"
                                      />

                    <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}"
                           AllowsTransparency="False" Focusable="False" PopupAnimation="Slide">
                        <Grid Name="DropDown" SnapsToDevicePixels="True" 
                              MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border x:Name="DropDownBorder" Background="{DynamicResource ribbonTitleFade}"
                                    BorderThickness="1" BorderBrush="{DynamicResource boPrimC}" />
                            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasItems" Value="false">
                        <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="Black"/>
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </Trigger>
                    <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                        <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="2"/>
                        <Setter TargetName="DropDownBorder" Property="Margin" Value="0"/>
                    </Trigger>
                    <Trigger Property="IsEditable" Value="true">
                        <Setter Property="IsTabStop" Value="false"/>
                        <!--<Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>-->
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
    </Style.Triggers>
</Style>

3 个答案:

答案 0 :(得分:3)

我认为您希望在ComboBox模板中更喜欢这样的内容:

<ContentPresenter Content="{TemplateBinding ComboBox.SelectionBoxItem}"
 ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"
 ContentStringFormat="{TemplateBinding ComboBox.SelectionBoxItemStringFormat}"
 Margin="{TemplateBinding Control.Padding}"
 HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
 VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
 SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />

答案 1 :(得分:0)

如果您的模板未明确处理DisplayMemberPath的值,则模板将不会使用它来更改ComboBoxItem。您必须直接在ControlTemplate中绑定该成员,或使用单独的DataTemplate

答案 2 :(得分:0)

我发现 BureauBlack主题的问题,它只是不应用 DisplayMemberPath ,只显示了对象名称。我有一个包含许多(15个以上)ComboBox的窗口,除了2个以外,所有这些都使用DisplayMemberPath正常工作。

我从ContentPresenters和GridViewRowPresenters中删除了以下内容:

Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"
ListBoxItem,ComboBoxItem,ListViewItem 中的

。可能还有更多需要修复,但这就是我们发现我们正在使用的所有内容。

使用Microsoft.Net 4.0 Framework。我没有试过4.5或3.5。