wpf组合框模板所选项目无法正常显示

时间:2017-03-28 08:25:28

标签: wpf templates

以下是我的组合框模板。外观很棒,绑定工作正常。但是,当我在列表中选择一个项目时 - 所选项目在框中没有正确显示 - 而是我得到一个长字符串值(即:common.Dto.CityDto),这实际上是我找到的Dto的文件位置为了绑定列表。奇怪的。 谢谢!

<ControlTemplate x:Key="GrayComboBox" TargetType="{x:Type ComboBox}">
        <Border BorderBrush="#dee0e3" BorderThickness="2" Background="#dee0e3" SnapsToDevicePixels="True" CornerRadius="7">
            <Grid FlowDirection="RightToLeft">
                <Themes:ClassicBorderDecorator x:Name="Border" BorderBrush="#00000000" BorderThickness="2" BorderStyle="Sunken" Background="#dee0e3">
                    <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{TemplateBinding IsDropDownOpen}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                        <Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=Border}">
                            <Border x:Name="DropDownBorder" BorderBrush="#dee0e3" BorderThickness="1" Background="#dee0e3">
                                <ScrollViewer x:Name="DropDownScrollViewer" >
                                    <Grid RenderOptions.ClearTypeHint="Enabled" >
                                        <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                            <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/>
                                        </Canvas>
                                        <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                    </Grid>
                                </ScrollViewer>
                            </Border>
                        </Themes:SystemDropShadowChrome>
                    </Popup>
                </Themes:ClassicBorderDecorator>
                <DockPanel Margin="2">
                    <FrameworkElement DockPanel.Dock="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/>
                    <Border x:Name="SelectedItemBorder" Margin="{TemplateBinding Padding}">
                        <ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </DockPanel>
                <ToggleButton ClickMode="Press" HorizontalAlignment="Right" Focusable="False" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="2" MinWidth="0" MinHeight="0" Width="Auto">
                    <ToggleButton.Style>
                        <Style TargetType="{x:Type ToggleButton}">
                            <Setter Property="MinWidth" Value="0"/>
                            <Setter Property="MinHeight" Value="0"/>
                            <Setter Property="Width" Value="Auto"/>
                            <Setter Property="Height" Value="Auto"/>
                            <Setter Property="Background" Value="Transparent"/>
                            <Setter Property="BorderBrush" Value="#00000000"/>
                            <Setter Property="BorderThickness" Value="2"/>
                            <Setter Property="Margin" Value="2,2,2,2" />
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                                        <DockPanel Background="{TemplateBinding Background}" LastChildFill="False" SnapsToDevicePixels="True">
                                            <Themes:ClassicBorderDecorator x:Name="Border" BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" BorderStyle="AltRaised" Background="Transparent" DockPanel.Dock="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                                                <!--<Path Data="M0,0L3.5,4 7,0z" Fill="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center"/>-->
                                                <fa:ImageAwesome Icon="CaretDown" Foreground="DarkGray" Width="13" Height="10" HorizontalAlignment="Center" VerticalAlignment="Center" />
                                            </Themes:ClassicBorderDecorator>
                                        </DockPanel>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsChecked" Value="True">
                                                <Setter Property="BorderStyle" TargetName="Border" Value="AltPressed"/>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                </ToggleButton>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelectionBoxHighlighted" Value="True"/>
                    <Condition Property="IsDropDownOpen" Value="False"/>
                </MultiTrigger.Conditions>
                <Setter Property="Foreground" Value="#006165"/>
            </MultiTrigger>
            <Trigger Property="IsSelectionBoxHighlighted" Value="True">
                <Setter Property="Background" TargetName="SelectedItemBorder" Value="#dee0e3"/>
            </Trigger>
            <Trigger Property="HasItems" Value="False">
                <Setter Property="MinHeight" TargetName="DropDownBorder" Value="95"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="DarkGray"/>
                <Setter Property="Background" Value="#dee0e3"/>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsGrouping" Value="True"/>
                    <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="False"/>
                </MultiTrigger.Conditions>
                <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
            </MultiTrigger>
            <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="True">
                <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
                <Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
            </Trigger>
            <Trigger Property="CanContentScroll" SourceName="DropDownScrollViewer" Value="False">
                <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/>
                <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

1 个答案:

答案 0 :(得分:0)

您忘记设置ContentTemplateSelector的{​​{1}}属性:

ContentPresenter