简体   繁体   中英

WPF Button template

I am trying to learn to customize WPF Buttons .

Earlier I used images as Content of the Button but I want to change that into XAML -based objects.

I am trying to fit a Path into the Button 's Content but can't get it right.

My version is below

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

but it doesn't end up too nicely and I cant get the ScaleTransform to work correctly:

在此处输入图片说明

I'm trying to get this kind of result:

在此处输入图片说明

Not sure, that transorm - is what you need, if you just want centered cross - this is more simple solution (no any changes, except Path in your Button):

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

Actually is Stretch property controls how is your shape is resized to fill its allocated space: MSDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM