首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过代码将MergedDictionaries添加到样式

通过代码将MergedDictionaries添加到样式
EN

Stack Overflow用户
提问于 2015-11-03 00:45:35
回答 1查看 146关注 0票数 2

我在资源文件中有这个style

代码语言:javascript
复制
<ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Colors.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <Style TargetType="TextBox" x:Key="StandardTextBox"/>
                <Setter Property="Foreground" Value="{StaticResource Color1}"/>
            </Style>
</ResourceDictionary>

(Colors.xaml包含我的brushes)

我使用style的代码

代码语言:javascript
复制
ResourceDictionary TetxboxStyles = new ResourceDictionary();
TetxboxStyles.Source = (new Uri("TextboxStyles.xaml", UriKind.RelativeOrAbsolute));
Resources.MergedDictionaries.Add(TetxboxStyles);
tb_input.Style = (Style)Find("StandardTextBox");

这没有问题,但当我通过代码而不是在TextboxStyles-File中动态添加Colors-Resource时,它不起作用:

代码语言:javascript
复制
ResourceDictionary TetxboxStyles = new ResourceDictionary();
TetxboxStyles.Source = (new Uri("TextboxStyles.xaml", UriKind.RelativeOrAbsolute));

//Adding the Colors.xaml Resource
ResourceDictionary Colors = new ResourceDictionary();
brushes.Source = (new Uri("Colors.xaml", UriKind.RelativeOrAbsolute));
TetxboxStyles.MergedDictionaries.Add(Colors);

Resources.MergedDictionaries.Add(TetxboxStyles);
tb_input.Style = (Style)Find("StandardTextBox");

输出-错误:

代码语言:javascript
复制
System.Windows.Markup.XamlParseException

"{DependencyProperty.UnsetValue}"

EN

回答 1

Stack Overflow用户

发布于 2015-11-03 01:22:01

我用DynamicResource替换了StaticResource,它可以工作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33482667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档