我正在从WPF应用程序的代码生成一个xaml,并希望为生成的xaml添加样式.我正在使用资源字典存储我的应用程序将访问的样式信息,并根据资源字典中的键应用于生成的xaml中的相应元素.现在我想将特定样式应用于生成的xaml中的所有文本框.我想要做的是在没有键的情况下在资源字典中创建这个样式,我想将这个样式资源添加到生成的xaml的资源中.我似乎找不到一种方法来在没有密钥的情况下将特定资源添加到此生成的xaml的资源字典中.我的问题是,是否可以这样做,即在没有该资源的密钥的情况下将资源添加到用户控件(或窗口等)的资源字典中?
例如,这是我想要生成的XAML:
<UserControl>
<UserControl.Resources>
<Style TargetType="{x:Type TextBox}">
<!--The TextBox style-->
</Style>
</UserControl.Resources>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
谢谢!
Duk*_*lus 17
所以,我找到了一种方法来做到这一点.在添加生成的xaml的资源而不是指定字符串键时,我使用了typeof(TextBox),这似乎有效.
UserControl.Resources.Add(typeof(TextBox), styleDictionary["TextBoxStyleKey"]);
Run Code Online (Sandbox Code Playgroud)
这导致:
<UserControl.Resources>
<Style TargetType="TextBox" x:Key="{x:Type TextBox}">
<!--The Style-->
</Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
有没有更好的方式(或任何其他方式)这样做?
归档时间: |
|
查看次数: |
5883 次 |
最近记录: |