默认组合框模板。弹出宽度

发布于 2024-10-14 20:52:53 字数 12355 浏览 1 评论 0原文

我想向默认组合框模板添加一些按钮,因此我覆盖了它的模板。

我使用这个:

 <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="l:RefreshableComboBox">
                <Grid Name="MainGrid" SnapsToDevicePixels="True">

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

                    <Popup  IsOpen="{Binding Path=IsDropDownOpen,  RelativeSource={RelativeSource TemplatedParent}}" 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 x:Name="ToggleButton" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"  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>
    </Setter>

由于某种原因,我的弹出窗口(PART_Popup)与toggleButton的宽度不同。它的宽度足以显示项目。如果我不覆盖模板弹出窗口,则其宽度与切换按钮相同。所以我不明白为什么我不能用标准模板得到同样的情况

I want add some buttons to default combobox template, so I override its template.

I use this:

 <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="l:RefreshableComboBox">
                <Grid Name="MainGrid" SnapsToDevicePixels="True">

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

                    <Popup  IsOpen="{Binding Path=IsDropDownOpen,  RelativeSource={RelativeSource TemplatedParent}}" 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 x:Name="ToggleButton" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"  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>
    </Setter>

By some reason my popup(PART_Popup) isn't the same width as toggleButton. It has width enough for displaying item. If I don't override template popup is the same width as toggleButton. So I can't understand why I can't get the same situation with standart template

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

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

发布评论

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

评论(1

童话里做英雄 2024-10-21 20:52:53

mwt:SystemDropShadowChromeMinWidth0 更改为:

MinWidth="{Binding ElementName=MainGrid,Path=ActualWidth}"

Change the MinWidth of mwt:SystemDropShadowChrome from 0 to:

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