组合框下拉不下降

发布于 2024-09-14 23:08:55 字数 13958 浏览 8 评论 0原文

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

它看起来很完美——直到我按下下拉按钮!什么也没发生。如果我从样式中删除模板,一切都会恢复正常。但这不是箭头填充颜色的变化,因为我也得到了与默认黑色箭头相同的行为。

相关问题意味着它可能与 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>

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 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别想她 2024-09-21 23:08:55

您已将 Popup 上的 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}}" ...

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}}" ...
锦爱 2024-09-21 23:08:55

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

我愿意。

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

Ido.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文