开发者

ComboBox drop down not dropping

开发者 https://www.devze.com 2023-01-11 16:25 出处:网络
I have a style for a ComboBox that I\'ve been using for awhile, but I wanted to tweak the color of the drop down arrow. To do this, I just took the default template for the comboBox and pasted it into

I have a style for a ComboBox that I've been using for awhile, but I wanted to tweak the color of the drop down arrow. To do this, I just took the default template for the comboBox and pasted it into my style, and changed the arrow color fill.

It looks perfect - until I hit the drop down! Nothing happens. If I cut the template from the style, everything works fine again. It isn't the arrow fill color change though, as I get the same behavior with it's default black arrow too.

A related question implies it may have something to do with the ToggleButton portion of the ttemplate, but I haven't solved it yet.

Cheers,

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>

Sample usage that works

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

Here is the template

    <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 IsChec开发者_开发知识库ked="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>


You have IsOpen set to False on the Popup and IsChecked set to False on the ToggleButton. Both of these are bound to ComboBox.IsDropDownOpen in the standard template, which is how the ToggleButton causes the Popup to open. Set the bindings like this:

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

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


Thank you, I had a problem because the Popup IsOpen binding was {TemplateBinding IsDropDownOpen} and not the binding you show here.

Ido.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号