当前位置:   article > 正文

WPF按钮相关

WPF按钮相关

跟着官网敲的按钮相关的内容,还涉及了wpf很多其他的知识

1.创建基本按钮

  1. <Grid>
  2. <StackPanel HorizontalAlignment="Left">
  3. <Button>Button1</Button>
  4. <Button>Button2</Button>
  5. <Button>Button3</Button>
  6. </StackPanel>
  7. </Grid>

2.设置基本属性

        有两种方式,直接在button后面添加 ,但是我这里有三个按钮,你在项目之中会有出现更多按钮的情况,所以我们这了选择另一种方式:写按钮的样式style

        这里在App.xaml下面进行创建style,即表示把这个style应用到全局:

  1. <!--创建样式-->
  2. <Style TargetType="Button">
  3. <Setter Property="Width" Value="90"></Setter>
  4. <Setter Property="Margin" Value="10"></Setter>
  5. </Style>

        这里的TargetType表示目标对象即button,下面的setter是设计器,property是button拥有的属性,value是我们设置的值.

3. 设置渐变静态资源并运用到按钮上面

        x:Key = "B1" 是上面资源的命名,下面引用的是{StaticResource B1}

  1. <LinearGradientBrush x:Key="B1" StartPoint="0,0" EndPoint="1,1">
  2. <GradientStop Color="DarkGray" Offset="0"></GradientStop>
  3. <GradientStop Color="#CCCCFF" Offset="0.5"></GradientStop>
  4. <GradientStop Color="DarkGray" Offset="1"></GradientStop>
  5. </LinearGradientBrush>
  6. <Style TargetType="{x:Type Button}">
  7. <Setter Property="Background" Value="{StaticResource B1}"></Setter>
  8. <Setter Property="Width" Value="80"></Setter>
  9. <Setter Property="Margin" Value="10"></Setter>
  10. </Style>

4.模板自定义按钮外观

        更改按钮呈现:自定义模板。 添加以下突出显示的标记。 此标记指定两个具有圆角的 Rectangle 元素,后跟一个 DockPanel。 DockPanel 用于托管按钮的 ContentPresenter。 ContentPresenter 显示按钮的内容。 在本演练中,内容为文本(“Button 1”、“Button 2”、“Button 3”)。 所有模板组件(矩形和 DockPanel)都布置在 Grid 内.

  1. <Setter Property="Template">
  2. <Setter.Value>
  3. <ControlTemplate TargetType="Button">
  4. <!--
  5. 使用了Grid布局容器来组织按钮的内部元素。
  6. Width和Height属性被绑定到模板自身的宽度和高度,确保Grid的大小与按钮的大小相匹配。
  7. ClipToBounds设置为True,意味着任何超出Grid边界的内容都会被裁剪。
  8. -->
  9. <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True">
  10. <!-- Outer Rectangle with rounded corners. -->
  11. <Rectangle x:Name="outerRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke="{TemplateBinding Background}" RadiusX="20" RadiusY="20" StrokeThickness="5" Fill="Transparent" />
  12. <!-- Inner Rectangle with rounded corners. -->
  13. <Rectangle x:Name="innerRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke="Transparent" StrokeThickness="20" Fill="{TemplateBinding Background}" RadiusX="20" RadiusY="20" />
  14. <!-- Present Content (text) of the button. -->
  15. <DockPanel Name="myContentPresenterDockPanel">
  16. <ContentPresenter x:Name="myContentPresenter" Margin="20" Content="{TemplateBinding Content}" TextBlock.Foreground="Black" />
  17. </DockPanel>
  18. </Grid>
  19. </ControlTemplate>
  20. </Setter.Value>
  21. </Setter>

 

5.设置触发器

  1. <ControlTemplate.Triggers>
  2. <!-- Set properties when mouse pointer is over the button. -->
  3. <Trigger Property="IsMouseOver" Value="True">
  4. <!-- Below are three property settings that occur when the
  5. condition is met (user mouses over button). -->
  6. <!-- Change the color of the outer rectangle when user mouses over it. -->
  7. <Setter Property ="Rectangle.Stroke" TargetName="outerRectangle"
  8. Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
  9. <!-- Sets the glass opacity to 1, therefore, the glass "appears" when user mouses over it. -->
  10. <Setter Property="Rectangle.Opacity" Value="1" TargetName="glassCube" />
  11. <!-- Makes the text slightly blurry as though you were looking at it through blurry glass. -->
  12. <Setter Property="ContentPresenter.BitmapEffect" TargetName="myContentPresenter">
  13. <Setter.Value>
  14. <BlurBitmapEffect Radius="1" />
  15. </Setter.Value>
  16. </Setter>
  17. </Trigger>
  18. <!-- Set properties when button has focus. -->
  19. <Trigger Property="IsFocused" Value="true">
  20. <Setter Property="Rectangle.Opacity" Value="1" TargetName="glassCube" />
  21. <Setter Property="Rectangle.Stroke" TargetName="outerRectangle" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
  22. <Setter Property="Rectangle.Opacity" Value="1" TargetName="glassCube" />
  23. </Trigger>
  24. </ControlTemplate.Triggers>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/277251
推荐阅读
相关标签
  

闽ICP备14008679号