WPF C#使用资源实现一致的外观
我正在开发一个WPF应用程序,试图定义在整个应用程序中使用的不同通用样式。我在这方面是个新手,所以我不知道是否有什么误解。不管怎么说,它是这样的: 例如,我创建了一个具有不同通用格式的资源字典。其中之一是边框的格式。定义如下:WPF C#使用资源实现一致的外观,c#,wpf,xaml,C#,Wpf,Xaml,我正在开发一个WPF应用程序,试图定义在整个应用程序中使用的不同通用样式。我在这方面是个新手,所以我不知道是否有什么误解。不管怎么说,它是这样的: 例如,我创建了一个具有不同通用格式的资源字典。其中之一是边框的格式。定义如下: <Style x:Key="MainBorderStyle" TargetType="{x:Type Border}"> <Setter Property="IsEnabled" Value="True" />
<Style x:Key="MainBorderStyle" TargetType="{x:Type Border}">
<Setter Property="IsEnabled"
Value="True" />
<Setter Property="Background"
Value="Transparent" />
<Setter Property="BorderBrush"
Value="Gold" />
<Setter Property="CornerRadius"
Value="6" />
<Setter Property="BorderThickness"
Value="2" />
<Setter Property="Padding"
Value="2" />
</Style>
现在,我想通过不同的元素(按钮、矩形、分组框等)在整个应用程序中使用这种格式
目前,我无法将边框格式应用于GroupBox控件。我还有一个预定义的GroupBox控件格式
在XAML代码的几个地方,我很幸运地通过使用代码片段将引用应用到了上面的边框设置
<Border Style="{StaticResource MainBorderStyle}" x:Key="SomeKeyID" />
但是,例如,对于groupbox,我不能让它工作,我必须在每个groupbox中重复提供笔刷、角点等的所有格式
有什么问题吗?如有任何建议,将不胜感激
致以最诚挚的问候。样式目标边框不能应用于其他类型的元素,如GroupBox 您可以创建新的MainGroupBoxStyle,从MainBorderStyle复制setters并将其应用于GroupBox:
<Style x:Key="MainGroupBoxStyle" TargetType="{x:Type GroupBox}">
<Setter Property="IsEnabled"
Value="True" />
<Setter Property="Background"
Value="Transparent" />
<Setter Property="BorderBrush"
Value="Gold" />
<Setter Property="BorderThickness"
Value="2" />
<Setter Property="Padding"
Value="2" />
</Style>
或使用不带TargetType的样式:
<Window.Resources>
<Style x:Key="MainBorderStyle">
<Setter Property="UIElement.IsEnabled"
Value="True" />
<Setter Property="Panel.Background"
Value="Transparent" />
<Setter Property="Border.BorderBrush"
Value="Gold" />
<Setter Property="Border.CornerRadius"
Value="16" />
<Setter Property="Border.BorderThickness"
Value="2" />
<Setter Property="Border.Padding"
Value="2" />
<Setter Property="Control.Padding"
Value="2" />
</Style>
</Window.Resources>
<StackPanel>
<Border Style="{StaticResource MainBorderStyle}">
<TextBlock Text="border"/>
</Border>
<GroupBox Style="{StaticResource MainBorderStyle}">
<TextBlock Text="groupBox"/>
</GroupBox>
</StackPanel>
GroupBox没有CornerRadius,但应用了其他属性注意,使用共享样式时,您依赖于框架的未记录的实现细节,例如,
控制.BorderBrush
和Border.BorderBrush
共享相同的依赖属性注册。无论如何,实施肯定不会改变。嗨,非常感谢你的帮助。正如我所希望的那样。顺致敬意,