用户控件的资源模板
Resource Template for UserControls
我正在开发一个包含多个 UserControls
的程序,它们应该遵循相同的视觉指南,因为它们将作为应用程序的 TabPages
向用户显示一些数据。
基本上,所有 UserControls
都有两列。在第 1 列中,我有一个标签描述哪个值属于实际控件,在第二列中跟随控件(TextBox
、DatePicker
、Checkbox
等)供用户设置:
当我设计第一个 UserControls
时,我最终得到了一堆 Styles
,我在 UserControl
s 资源中设置了这样的资源,因此所有标签都将具有宽度相同,文本右对齐,反之亦然:
<UserControl.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Width" Value="250"/>
<Setter Property="DockPanel.Dock" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalContentAlignment" Value="Right"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="DisplayMemberPath" Value="Value"/>
<Setter Property="SelectedValuePath" Value="Key"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type DockPanel}">
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
</UserControl.Resources>
现在,因为我要创建所有其他 60+ UserControls
,所以我想避免将完全相同的代码一遍又一遍地添加到每个 UserControl
,只是为了实现视觉风格的一致性,比如标签的相同固定宽度等等,因为这将很难维护(想象一下客户要求所有标签的宽度都减少 50px...)。
想知道是否有任何方法可以在应用程序的某处设置此格式设置,然后 "import" 我的 UserControls
的这些设置如下所示:
主文件
<Master.Template1>
<Style TargetType="{x:Type Label}">
...
</Style>
<Style TargetType="{x:Type TextBox}">
...
</Style>
<Style TargetType="{x:Type ComboBox}">
...
</Style>
<Style TargetType="{x:Type DockPanel}">
...
</Style>
</Master.Template1>
其他UC
<UserControl.Resources>
<using Style:Master.Template1/>
</UserControl.Resources>
我已经阅读了一些关于此的内容 Application.ResourceDictionary
,但我不确定我是否在正确的轨道上,因为我不想弄乱代码隐藏文件。
或者我应该创建自己的控件,该控件 从 UserControl
派生 已经在其资源中设置了所有这些值?
向项目添加一个新的 ResourceDictionary
文件。将您的样式放入 ResourceDictionary
.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Label}">
<Setter Property="Width" Value="250"/>
<Setter Property="DockPanel.Dock" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalContentAlignment" Value="Right"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="DisplayMemberPath" Value="Value"/>
<Setter Property="SelectedValuePath" Value="Key"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type DockPanel}">
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
</ResourceDictionary>
然后将资源字典添加到您的 UserControl.Resources
<UserControl.Resources>
<ResourceDictionary Source="MyResources.xaml"/>
</UserControl.Resources>
编辑:
Glen Thomas 和我自己同时设法回答了这个问题。我仍然留下我自己的答案,因为 the article on MSDN 包含一些有用的进一步信息,即将到来的用户可能会发现了解 如何 在第一个中添加 ResourceDictionary
很有用地点。
当然这是可能的,而且就像我希望的那样简单:ResourceDictionarys
是正确的选择!
这个 article in the MSDN 让我找到了正确的方向。
我刚刚在我的程序中添加了一个新的 ResourceDictionary
并将参考代码放入其中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Label}">
<Setter Property="Width" Value="250"/>
<Setter Property="DockPanel.Dock" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalContentAlignment" Value="Right"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="DisplayMemberPath" Value="Value"/>
<Setter Property="SelectedValuePath" Value="Key"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type DockPanel}">
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
</ResourceDictionary>
之后,我可以通过将 UserControl.Resources
替换为以下内容,将字典“导入”到我的 UserControl
中:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
瞧瞧:格式化控件能够选择是否应用资源“模板”以及修改共享值的中心位置。
完美!
我正在开发一个包含多个 UserControls
的程序,它们应该遵循相同的视觉指南,因为它们将作为应用程序的 TabPages
向用户显示一些数据。
基本上,所有 UserControls
都有两列。在第 1 列中,我有一个标签描述哪个值属于实际控件,在第二列中跟随控件(TextBox
、DatePicker
、Checkbox
等)供用户设置:
当我设计第一个 UserControls
时,我最终得到了一堆 Styles
,我在 UserControl
s 资源中设置了这样的资源,因此所有标签都将具有宽度相同,文本右对齐,反之亦然:
<UserControl.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Width" Value="250"/>
<Setter Property="DockPanel.Dock" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalContentAlignment" Value="Right"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="DisplayMemberPath" Value="Value"/>
<Setter Property="SelectedValuePath" Value="Key"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type DockPanel}">
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
</UserControl.Resources>
现在,因为我要创建所有其他 60+ UserControls
,所以我想避免将完全相同的代码一遍又一遍地添加到每个 UserControl
,只是为了实现视觉风格的一致性,比如标签的相同固定宽度等等,因为这将很难维护(想象一下客户要求所有标签的宽度都减少 50px...)。
想知道是否有任何方法可以在应用程序的某处设置此格式设置,然后 "import" 我的 UserControls
的这些设置如下所示:
主文件
<Master.Template1>
<Style TargetType="{x:Type Label}">
...
</Style>
<Style TargetType="{x:Type TextBox}">
...
</Style>
<Style TargetType="{x:Type ComboBox}">
...
</Style>
<Style TargetType="{x:Type DockPanel}">
...
</Style>
</Master.Template1>
其他UC
<UserControl.Resources>
<using Style:Master.Template1/>
</UserControl.Resources>
我已经阅读了一些关于此的内容 Application.ResourceDictionary
,但我不确定我是否在正确的轨道上,因为我不想弄乱代码隐藏文件。
或者我应该创建自己的控件,该控件 从 UserControl
派生 已经在其资源中设置了所有这些值?
向项目添加一个新的 ResourceDictionary
文件。将您的样式放入 ResourceDictionary
.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Label}">
<Setter Property="Width" Value="250"/>
<Setter Property="DockPanel.Dock" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalContentAlignment" Value="Right"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="DisplayMemberPath" Value="Value"/>
<Setter Property="SelectedValuePath" Value="Key"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type DockPanel}">
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
</ResourceDictionary>
然后将资源字典添加到您的 UserControl.Resources
<UserControl.Resources>
<ResourceDictionary Source="MyResources.xaml"/>
</UserControl.Resources>
编辑:
Glen Thomas 和我自己同时设法回答了这个问题。我仍然留下我自己的答案,因为 the article on MSDN 包含一些有用的进一步信息,即将到来的用户可能会发现了解 如何 在第一个中添加 ResourceDictionary
很有用地点。
当然这是可能的,而且就像我希望的那样简单:ResourceDictionarys
是正确的选择!
这个 article in the MSDN 让我找到了正确的方向。
我刚刚在我的程序中添加了一个新的 ResourceDictionary
并将参考代码放入其中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Label}">
<Setter Property="Width" Value="250"/>
<Setter Property="DockPanel.Dock" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalContentAlignment" Value="Right"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="DisplayMemberPath" Value="Value"/>
<Setter Property="SelectedValuePath" Value="Key"/>
<Setter Property="Margin" Value="2"/>
</Style>
<Style TargetType="{x:Type DockPanel}">
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
</ResourceDictionary>
之后,我可以通过将 UserControl.Resources
替换为以下内容,将字典“导入”到我的 UserControl
中:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
瞧瞧:格式化控件能够选择是否应用资源“模板”以及修改共享值的中心位置。
完美!