Wpf 设置组合框的样式,以移除内部填充,并去除圆角。怎么用?

Wpf 设置组合框的样式,以移除内部填充,并去除圆角。怎么用?,wpf,xaml,styles,Wpf,Xaml,Styles,我用Blend复制了这个样式,做了我需要的大部分修改,但我仍然坚持要完全去掉圆角,去掉周围的空白。 带有arrouw的按钮应为18,整个下拉菜单应为18高。现在,当按钮未居中时,周围有填充和白色。再加上,右边这个四舍五入 这是我的风格: <Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}"> <Setter Property="OverridesDe

我用Blend复制了这个样式,做了我需要的大部分修改,但我仍然坚持要完全去掉圆角,去掉周围的空白。 带有arrouw的按钮应为18,整个下拉菜单应为18高。现在,当按钮未居中时,周围有填充和白色。再加上,右边这个四舍五入

这是我的风格:

<Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}">
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="IsTabStop" Value="false"/>
            <Setter Property="Focusable" Value="false"/>
            <Setter Property="ClickMode" Value="Press"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Microsoft_Windows_Themes:ButtonChrome Height="18"
                            x:Name="Chrome" RoundCorners="False" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            Background="{TemplateBinding Background}" 
                            RenderMouseOver="{TemplateBinding IsMouseOver}" 
                            RenderPressed="{TemplateBinding IsPressed}" SnapsToDevicePixels="true">
                            <Border
                                BorderBrush="#a6a6a6" BorderThickness="1"
                                HorizontalAlignment="Right" Width="18" Height="18">
                                <Border.Background>                                    
                                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                        <GradientStop Color="#899ba5" />
                                        <GradientStop Color="#4f6473" Offset="1" />
                                    </LinearGradientBrush>                                    
                                </Border.Background>
                                <Grid>
                                    <Path x:Name="Arrow" Fill="#dcdcdc" HorizontalAlignment="Center" VerticalAlignment="Center">
                                        <Path.Data>
                                            <Geometry>M 0 0 L 3.5 4 L 7 0 Z</Geometry>
                                        </Path.Data>
                                    </Path>
                                </Grid>
                            </Border>                            
                        </Microsoft_Windows_Themes:ButtonChrome>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>        
        <Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}">
            <Setter Property="Height" Value="18" />
            <Setter Property="Foreground" Value="#1a3e55"/>
            <Setter Property="Background" Value="#f7f7f7"/>
            <Setter Property="BorderBrush" Value="#a6a6a6"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>

            <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
            <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBox}">
                        <Grid x:Name="MainGrid" 
                            SnapsToDevicePixels="true">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
                            </Grid.ColumnDefinitions>
                            <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                                <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}">
                                    <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                                        <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>
                                </Microsoft_Windows_Themes:SystemDropShadowChrome>
                            </Popup>
                            <ToggleButton 
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                Background="{TemplateBinding Background}" 
                                Grid.ColumnSpan="2" 
                                IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
                                Style="{StaticResource ComboBoxReadonlyToggleButton}"/>
                            <ContentPresenter 
                                ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
                                ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
                                Content="{TemplateBinding SelectionBoxItem}" 
                                ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" 
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                IsHitTestVisible="false" 
                                Margin="{TemplateBinding Padding}" 
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>           
        </Style>    

M 0 0 L 3.5 4 L 7 0 Z
好吧,这次我终于答对了

所有这些都在您的纽扣样式模板中。我用这个答案来解释如何改变这种风格。从此处提供的链接下载类文件:

首先,让我们去掉圆角。

将切换按钮的样式更新为按钮chrome类的本地副本,并设置RoundedCorners=“false”

我将InnerBorder函数(第180行)更改为:

现在让我们来处理这个填充。

这一个花了一些时间才发现,但我在函数ArrangeOverride(第40行)中找到了它。只需删除以下函数,它将不再覆盖放置

protected override Size ArrangeOverride(Size finalSize)
结果:

为了证明它确实有效


您设置的圆角=false仅用于按钮-将该属性应用于整个控件。至于填充物,我不知道。您是否尝试添加padding=“0”属性?控件没有圆角属性我知道了!!这真是个有趣的问题。对于第一次的评论和回答,很抱歉,乍一看,这似乎很简单,但事实证明,这相当复杂。:)哇!谢谢。我确实看到了这个答案,但我的问题是“为什么一定要这么难?”在我看来,XAML的所有内容都是关于我想要的任何样式的简化。创建定制的铬只是为了我需要的风格似乎有点过火。顺便说一句,我试图设计复选框,却遇到了完全相同的问题是的,我同意这是过火了。:)但在尝试了几种不同的方法之后,我只是想看看这是否可行。
if (this.RoundCorners)
{
    Rect rectangle = new Rect(bounds.Left + 0.5, bounds.Top + 0.5, bounds.Width - 1.0, bounds.Height - 1.0);
    if (base.IsEnabled && (pen != null))
    {
        dc.DrawRoundedRectangle(null, pen, rectangle, 2.75, 2.75);
    }
    if (borderOverlayPen != null)
    {
        dc.DrawRoundedRectangle(null, borderOverlayPen, rectangle, 2.75, 2.75);
    }
}
else
{
    Rect rectangle = new Rect(bounds.Left + 0.5, bounds.Top + 0.5, bounds.Width - 1.0, bounds.Height - 1.0);
    if (base.IsEnabled && (pen != null))
    {
        dc.DrawRectangle(null, pen, rectangle);
    }
    if (borderOverlayPen != null)
    {
        dc.DrawRectangle(null, borderOverlayPen, rectangle);
    }
}
private void DrawInnerBorder(DrawingContext dc, ref Rect bounds)
{
    if (DisableInnerBorder == false && ((base.IsEnabled || this.RoundCorners) && ((bounds.Width >= 4.0) && (bounds.Height >= 4.0))))
    {
        Pen innerBorderPen = this.InnerBorderPen;
        if (innerBorderPen != null)
        {
            if (this.RoundCorners)
            {
                dc.DrawRoundedRectangle(null, innerBorderPen, new Rect(bounds.Left + 1.5, bounds.Top + 1.5, bounds.Width - 3.0, bounds.Height - 3.0), 1.75, 1.75);
            }
            else
            {
                dc.DrawRoundedRectangle(null, innerBorderPen, new Rect(bounds.Left + 1.5, bounds.Top + 1.5, bounds.Width - 3.0, bounds.Height - 3.0), 0, 0);
            }
        }
    }
}
protected override Size ArrangeOverride(Size finalSize)