【问题标题】:WPF Button templateWPF 按钮模板
【发布时间】:2015-04-02 10:00:44
【问题描述】:

我正在尝试学习自定义 WPF Buttons

之前我将图像用作ButtonContent,但我想将其更改为基于XAML 的对象。

我正在尝试将Path 放入ButtonContent,但无法正确设置。

我的版本在下面

<Window.Resources>
     <Style x:Key="MyStyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Rectangle RadiusY="2" RadiusX="2" Stroke="Black" Fill="Gray" />
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="0,0,0,0" Visibility="Visible"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Height" Value="22" />
        <Setter Property="Width" Value="22" />
    </Style>
</Window.Resources>

<Button VerticalAlignment="Center" HorizontalAlignment="Center" Style="{DynamicResource MyStyle}">
    <Path Fill="Black" RenderTransformOrigin="0.375,0.375"  >
        <Path.RenderTransform>
            <ScaleTransform ScaleX="0.375" ScaleY="0.375" CenterX="0" CenterY="0"/>
        </Path.RenderTransform>
        <Path.Data>
            <PathGeometry Figures="M22,18L22,8 18,8 18,18 8,18 8,22 18,22 18,32 22,32 22,22 32,22 32,18z" />
        </Path.Data>
    </Path>
</Button>

但结果不太好,我无法让 ScaleTransform 正常工作:

我正在尝试得到这样的结果:

【问题讨论】:

    标签: wpf button styles


    【解决方案1】:

    不确定,transorm - 是你需要的,如果你只是想要居中的十字 - 这是更简单的解决方案(没有任何变化,除了按钮中的路径):

            <Path
                Fill="Black" 
                Stretch="Uniform"
                Margin="7"
                Data="M22,18L22,8 18,8 18,18 8,18 8,22 18,22 18,32 22,32 22,22 32,22 32,18z"
                />
    

    实际上是 Stretch 属性控制如何调整形状的大小以填充其分配的空间:MSDN

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 2014-11-05
      相关资源
      最近更新 更多