ComboBox下拉不下降

时间:2010-08-16 23:31:05

标签: wpf silverlight combobox styles controltemplate

我有一个ComboBox的样式,我已经使用了一段时间,但我想调整下拉箭头的颜色。为此,我只使用了comboBox的默认模板并将其粘贴到我的样式中,并更改了箭头颜色填充。

它看起来很完美 - 直到我击中下降!什么都没发生。如果我从样式中剪切模板,一切都可以正常工作。这不是箭头填充颜色的变化,因为我也得到了与它的默认黑色箭头相同的行为。

related问题意味着它可能与ttemplate的ToggleButton部分有关,但我还没有解决它。

干杯,
Berryl

<!--ComboBoxStyle-->
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
    <Setter Property="Background" Value="{StaticResource headerBrush}" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="BorderBrush" Value="{StaticResource headerBorderBrush}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="MinWidth" Value="100" />
    <Setter Property="MaxWidth" Value="175" />
    <Setter Property="MinHeight" Value="25" />
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Padding" Value="5" />
    <Setter Property="Margin" Value="3" />
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />

    <Setter Property="Template">
        <Setter.Value>

            ... std combobox template with white arrow <================

        </Setter.Value>
    </Setter>

有效的示例用法

<ComboBox Style="{StaticResource ComboBoxStyle}" 
          ItemContainerStyle="{StaticResource ComboBoxItemStyle}"
          ItemsSource="{Binding Path=Departments}" 
          SelectedItem="{Binding Path=Department, Mode=TwoWay}" 
          ...
 />

这是模板

    <Setter Property="Template">
        <Setter.Value>

            <ControlTemplate TargetType="ComboBox" 
                 xmlns:s="clr-namespace:System;assembly=mscorlib" 
                 xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
                 >
                <Grid Name="MainGrid" SnapsToDevicePixels="True">

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="0" MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" />
                    </Grid.ColumnDefinitions>

                    <Popup IsOpen="False" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" AllowsTransparency="True" Name="PART_Popup" Margin="1,1,1,1" Grid.ColumnSpan="2">
                        <mwt:SystemDropShadowChrome Color="#00FFFFFF" Name="Shdw" MinWidth="0" MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}">
                            <Border BorderThickness="1,1,1,1" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" Name="DropDownBorder">
                                <ScrollViewer Name="DropDownScrollViewer">
                                    <Grid RenderOptions.ClearTypeHint="Enabled">
                                        <Canvas Width="0" Height="0" HorizontalAlignment="Left" VerticalAlignment="Top">
                                            <Rectangle Fill="{x:Null}" Name="OpaqueRect" Width="Auto" Height="Auto" />
                                        </Canvas>
                                        <ItemsPresenter Name="ItemsPresenter" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Contained" />
                                    </Grid>
                                </ScrollViewer>
                            </Border>
                        </mwt:SystemDropShadowChrome>
                    </Popup>
                    <ToggleButton IsChecked="False" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Grid.ColumnSpan="2">
                        <ToggleButton.Style>
                            <Style TargetType="ToggleButton">
                                <Style.Resources>
                                    <ResourceDictionary />
                                </Style.Resources>
                                <Setter Property="FrameworkElement.OverridesDefaultStyle">
                                    <Setter.Value>
                                        <s:Boolean>True</s:Boolean>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="KeyboardNavigation.IsTabStop">
                                    <Setter.Value>
                                        <s:Boolean>False</s:Boolean>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="UIElement.Focusable">
                                    <Setter.Value>
                                        <s:Boolean>False</s:Boolean>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="ButtonBase.ClickMode">
                                    <Setter.Value>
                                        <x:Static Member="ClickMode.Press" />
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="Control.Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="ToggleButton">
                                            <mwt:ButtonChrome Background="{TemplateBinding Panel.Background}" 
                                                  BorderBrush="{TemplateBinding Border.BorderBrush}" 
                                                  RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}" 
                                                  RenderPressed="{TemplateBinding ButtonBase.IsPressed}" 
                                                  Name="Chrome" SnapsToDevicePixels="True">
                                                <Grid Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" HorizontalAlignment="Right">
                                                    <Path Data="M0,0L3.5,4 7,0z" Fill="#FF000000" Name="Arrow" Margin="3,1,0,0" 
                                              HorizontalAlignment="Center" VerticalAlignment="Center" />
                                                </Grid>
                                            </mwt:ButtonChrome>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="ToggleButton.IsChecked">
                                                    <Setter Property="mwt:ButtonChrome.RenderPressed" TargetName="Chrome">
                                                        <Setter.Value>
                                                            <s:Boolean>True</s:Boolean>
                                                        </Setter.Value>
                                                    </Setter>
                                                    <Trigger.Value>
                                                        <s:Boolean>True</s:Boolean>
                                                    </Trigger.Value>
                                                </Trigger>
                                                <Trigger Property="UIElement.IsEnabled">
                                                    <Setter Property="Shape.Fill" TargetName="Arrow">
                                                        <Setter.Value>
                                                            <SolidColorBrush>#FFAFAFAF</SolidColorBrush>
                                                        </Setter.Value>
                                                    </Setter>
                                                    <Trigger.Value>
                                                        <s:Boolean>False</s:Boolean>
                                                    </Trigger.Value>
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ToggleButton.Style>
                    </ToggleButton>
                    <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}" IsHitTestVisible="False" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="Popup.HasDropShadow" SourceName="PART_Popup">
                        <Setter Property="FrameworkElement.Margin" TargetName="Shdw">
                            <Setter.Value>
                                <Thickness>0,0,5,5</Thickness>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="mwt:SystemDropShadowChrome.Color" TargetName="Shdw">
                            <Setter.Value>
                                <Color>#71000000</Color>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="ItemsControl.HasItems">
                        <Setter Property="FrameworkElement.Height" TargetName="DropDownBorder">
                            <Setter.Value>
                                <s:Double>95</s:Double>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>False</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="UIElement.IsEnabled">
                        <Setter Property="TextElement.Foreground">
                            <Setter.Value>
                                <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Panel.Background">
                            <Setter.Value>
                                <SolidColorBrush>#FFF4F4F4</SolidColorBrush>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>False</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="ItemsControl.IsGrouping">
                        <Setter Property="ScrollViewer.CanContentScroll">
                            <Setter.Value>
                                <s:Boolean>False</s:Boolean>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer">
                        <Setter Property="Canvas.Top" TargetName="OpaqueRect">
                            <Setter.Value>
                                <Binding Path="VerticalOffset" ElementName="DropDownScrollViewer" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Canvas.Left" TargetName="OpaqueRect">
                            <Setter.Value>
                                <Binding Path="HorizontalOffset" ElementName="DropDownScrollViewer" />
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>False</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

        </Setter.Value>

2 个答案:

答案 0 :(得分:3)

在Popog上将IsOpen设置为False,在ToggleButton上将IsChecked设置为False。这两个都绑定到标准模板中的ComboBox.IsDropDownOpen,这是ToggleButton导致Popup打开的方式。像这样设置绑定:

<Popup IsOpen="{Binding Path=IsDropDownOpen,
    RelativeSource={RelativeSource TemplatedParent}}"  ...

<ToggleButton IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, 
    RelativeSource={RelativeSource TemplatedParent}}" ...

答案 1 :(得分:0)

谢谢,我遇到了问题,因为Popup IsOpen绑定是{TemplateBinding IsDropDownOpen},而不是你在这里显示的绑定。

伊多。