My WPF UserControl contains two stack panels and each of them contains labels, text boxes and radio buttons.
I would like to set VerticalAlignment
property to Center
to all controls in my UserControl with as little code as possible.
我的WPF UserControl包含两个堆栈面板,每个面板包含标签,文本框和单选按钮。我想使用尽可能少的代码将VerticalAlignment属性设置为Center to我的UserControl中的所有控件。
Now I have following solutions:
现在我有以下解决方案:
- brute force - put
VerticalAlignment="Center"
in each control - 暴力 - 在每个控件中放置VerticalAlignment =“Center”
- define one style for
FrameworkElement
and apply it directly - 为FrameworkElement定义一种样式并直接应用它
- define styles for each type of the controls on user control (this needs 3 style definitions, but automatically applies style to the control)
- 为用户控件上的每种控件类型定义样式(这需要3个样式定义,但会自动将样式应用于控件)
These three solutions need too much code.
Is there any other way to write this?
I hoped that defining style for FrameworkElement
would automatically set property to all controls, but it does not work.
这三个解决方案需要太多代码。还有其他方法可以写这个吗?我希望FrameworkElement的定义样式会自动将属性设置为所有控件,但它不起作用。
Here is snippet of my current XAML (I omitted second, very similar stack panel):
这是我当前XAML的片段(我省略了第二个,非常相似的堆栈面板):
<UserControl.Resources>
<Style x:Key="BaseStyle" TargetType="FrameworkElement">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<TextBlock Style="{StaticResource BaseStyle}" Text="Value:" />
<RadioButton Style="{StaticResource BaseStyle}">Standard</RadioButton>
<RadioButton Style="{StaticResource BaseStyle}">Other</RadioButton>
<TextBox Style="{StaticResource BaseStyle}" Width="40"/>
</StackPanel>
</Grid>
Edit:
Re Will's comment: I really hate idea of writing control formatting code in codebehind. XAML should be sufficient for this really simple user control.
编辑:Re Will的评论:我真的很讨厌在代码隐藏中编写控件格式代码的想法。 XAML应该足以实现这种非常简单的用户控制。
Re Muad'Dib's comment: Controls I use in my user control are derived from FrameworkElement
, so this is not an issue here.
Re Muad'Dib的评论:我在用户控件中使用的控件是从FrameworkElement派生的,所以这不是问题。
1 个解决方案
#1
9
I had come across the same conundrum awhile ago as well. Not sure if this is the "best" way, but it was easy enough to manage by defining your base style and then creating separate styles for each control on the page that inherited from the base style:
我前一段时间也遇到过同样的难题。不确定这是否是“最佳”方式,但通过定义基本样式然后为从基本样式继承的页面上的每个控件创建单独的样式,可以轻松管理:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="500" Height="300" Background="OrangeRed">
<Page.Resources>
<Style TargetType="FrameworkElement" x:Key="BaseStyle">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="0,0,5,0" />
</Style>
<Style TargetType="TextBlock" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="RadioButton" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}" />
</Page.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Value:" />
<RadioButton>Standard</RadioButton>
<RadioButton>Other</RadioButton>
<TextBox Width="75"/>
</StackPanel>
</Grid>
</Page>
#1
9
I had come across the same conundrum awhile ago as well. Not sure if this is the "best" way, but it was easy enough to manage by defining your base style and then creating separate styles for each control on the page that inherited from the base style:
我前一段时间也遇到过同样的难题。不确定这是否是“最佳”方式,但通过定义基本样式然后为从基本样式继承的页面上的每个控件创建单独的样式,可以轻松管理:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="500" Height="300" Background="OrangeRed">
<Page.Resources>
<Style TargetType="FrameworkElement" x:Key="BaseStyle">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="0,0,5,0" />
</Style>
<Style TargetType="TextBlock" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="RadioButton" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}" />
</Page.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Value:" />
<RadioButton>Standard</RadioButton>
<RadioButton>Other</RadioButton>
<TextBox Width="75"/>
</StackPanel>
</Grid>
</Page>