使用 Window.Shapes.Path 未正确绘制 WPF Buttonstyle
WPF Buttonstyle not properly drawn using Window.Shapes.Path
我目前正在学习使用 MVVM 模式的 WPF,当我使用 Windows.Shapes.Path 绘制按钮时,我有一个 'weird' 行为。
通过一些测试,我将问题缩小到纯 WPF。这是我的 XAML:
<Window x:Class="ButtonStyleTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ButtonStyleTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="Content">
<Setter.Value>
<Path Data="M1,9 L9,1 M1,1 L9,9" Stroke="White" StrokeThickness="2" />
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<WrapPanel Grid.Column="0" >
<Button Style="{StaticResource MyButtonStyle}"/>
<Button Style="{StaticResource MyButtonStyle}"/>
</WrapPanel>
<WrapPanel Grid.Column="1" >
<Button Style="{StaticResource MyButtonStyle}"/>
<Button Style="{StaticResource MyButtonStyle}"/>
</WrapPanel>
</Grid>
不幸的是,我不允许 post 图片,因为我需要 10 个声望:(
所以我只是 post 链接 - 对于给您带来的不便,我们深表歉意。
运行 程序看起来像这样
http://imageshack.com/a/img923/8541/HBAHyi.png
调整 window 大小后,按钮的绘制方式有所不同
http://imageshack.com/a/img922/4600/gzEXWj.png
如果我使用字符串"X"作为内容,即
<Window.Resources>
<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="Content" Value="x"/>
</Style>
</Window.Resources>
每个按钮都有自己的 "x"。
在我的实际应用程序中,我找到了一个解决方法。这些按钮绑定到一个 ObservableCollection。如果我在 Datatemplate 中指定 <Path Data... />
标签,一切都会正确显示。将其定义为按钮样式时,它看起来像上图。
但是,我想了解为什么路径 属性 在样式中对我不起作用。
改变你的风格,让它看起来像:
<Style x:Key="MyButtonStyle" x:Shared="False" TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="Content">
<Setter.Value>
<Path Data="M1,9 L9,1 M1,1 L9,9" Stroke="White" StrokeThickness="2" />
</Setter.Value>
</Setter>
</Style>
然后它将按照您想要的方式工作。
这里是关于x:Shared Attribute
的一些细节
基本上它的作用是在设置为 FALSE
时创建样式的新实例。您需要为每个按钮创建一个 PATH
的新实例。
我目前正在学习使用 MVVM 模式的 WPF,当我使用 Windows.Shapes.Path 绘制按钮时,我有一个 'weird' 行为。
通过一些测试,我将问题缩小到纯 WPF。这是我的 XAML:
<Window x:Class="ButtonStyleTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ButtonStyleTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="Content">
<Setter.Value>
<Path Data="M1,9 L9,1 M1,1 L9,9" Stroke="White" StrokeThickness="2" />
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<WrapPanel Grid.Column="0" >
<Button Style="{StaticResource MyButtonStyle}"/>
<Button Style="{StaticResource MyButtonStyle}"/>
</WrapPanel>
<WrapPanel Grid.Column="1" >
<Button Style="{StaticResource MyButtonStyle}"/>
<Button Style="{StaticResource MyButtonStyle}"/>
</WrapPanel>
</Grid>
不幸的是,我不允许 post 图片,因为我需要 10 个声望:( 所以我只是 post 链接 - 对于给您带来的不便,我们深表歉意。
运行 程序看起来像这样
http://imageshack.com/a/img923/8541/HBAHyi.png
调整 window 大小后,按钮的绘制方式有所不同
http://imageshack.com/a/img922/4600/gzEXWj.png
如果我使用字符串"X"作为内容,即
<Window.Resources>
<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="Content" Value="x"/>
</Style>
</Window.Resources>
每个按钮都有自己的 "x"。
在我的实际应用程序中,我找到了一个解决方法。这些按钮绑定到一个 ObservableCollection。如果我在 Datatemplate 中指定 <Path Data... />
标签,一切都会正确显示。将其定义为按钮样式时,它看起来像上图。
但是,我想了解为什么路径 属性 在样式中对我不起作用。
改变你的风格,让它看起来像:
<Style x:Key="MyButtonStyle" x:Shared="False" TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="Content">
<Setter.Value>
<Path Data="M1,9 L9,1 M1,1 L9,9" Stroke="White" StrokeThickness="2" />
</Setter.Value>
</Setter>
</Style>
然后它将按照您想要的方式工作。
这里是关于x:Shared Attribute
的一些细节基本上它的作用是在设置为 FALSE
时创建样式的新实例。您需要为每个按钮创建一个 PATH
的新实例。