繁体   English   中英

如何在WPF应用程序中创建椭圆形按钮?

[英]How to create oval button in WPF application?

正是作为主题,如何在WPF应用程序中创建椭圆形按钮?

像这样:

<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse Width="64" Height="32" Fill="Blue" />
<ContentControl Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

我没有测试代码,但是您应该知道这个主意:)

编辑:椭圆当然有一个Fill属性而不是Background。

安德烈(Andrej)

以下代码可以直接在您的窗口中运行,并支持IsPressed和IsMouseOver触发器。

<Window.Resources>

    <ControlTemplate x:Key="ButtonControlTemplate" TargetType="{x:Type Button}">
        <Grid>
            <Ellipse Fill="White" Stroke="Black" VerticalAlignment="Top" Height="32" x:Name="theEllipse"/>
            <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Fill" Value="Yellow" TargetName="theEllipse"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Fill" Value="Gray" TargetName="theEllipse"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

</Window.Resources>

<Grid x:Name="LayoutRoot">
    <Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Content="Button" Template="{DynamicResource ButtonControlTemplate}"/>
</Grid>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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