在我的LOB应用程序中,我通常会使用包含大量不同文本块和文本框的容器,以便用户输入数据.通常我需要对每个控件应用一定的余量或垂直/水平对齐.
假设我的表单上有Grid,看起来像这样(为简洁起见,很多标记都被删除了):
<Grid>
<Grid.ColumnDefinitions.../>
<Grid.RowDefinitions.../>
<TextBlock Text="MyLabel" />
<TextBox Text={Binding ...}/>
.
'
<!-- Repated a bunch more times along with all of the Grid.Row, Grid.Column definitions -->
</Grid>
Run Code Online (Sandbox Code Playgroud)
现在让我们说我需要网格中包含的每个项目都有Margin ="3,1"VerticalContentAlignment ="Left"VerticalAlignment ="Center".有几种方法可以实现这一目标:
<Setter Property="Frameworkelement.Margin" Value="3,1" />Not bad ...它正确地将样式应用于其内容中的每个元素,但也将它直接应用于Grid本身......不完全是我想要的.那么你采取什么方法?为什么?什么效果最好?
您可以将"全局"样式放Resources入网格部分,从而限制其影响.要在不同位置重用"全局"样式,请将它们放入非默认资源字典中并将其包含为MergedDictionary:
在Styles.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="{x:Type ...}"> ... </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
形式如下:
<Grid>
<Grid.ColumnDefinitions.../>
<Grid.RowDefinitions.../>
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- other resources here -->
</ResourceDictionary>
</Grid.Resources>
...
</Grid>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
453 次 |
| 最近记录: |