设置样式为静态资源为根控制元素 [英] Setting a Style to a Static Resource for a root Control element

查看:205
本文介绍了设置样式为静态资源为根控制元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置root 控制元素的样式在我的XAML到的StaticResource 定义外部的ResourceDictionary ,但我收到错误消息:

I'm trying to set the style of the root Control element in my XAML to a StaticResource defined in an external ResourceDictionary, however I receive the error:

资源MyControlStyle无法得到解决。

<UserControl
    ...
    >

    <UserControl.Resources>
        <ResourceDictionary Source="..\Styles\MyStyles.xaml" />
    </UserControl.Resources>

    <UserControl.Style>
        <StaticResource ResourceKey="MyControlStyle"/>
   </UserControl.Style>

</UserControl>

除了设置样式的XAML文件,使得使用我的控制,如何实现这一点?我希望能够从在用户控件设置样式,这样我可以看到设计师为用户控件的样式的影响

Aside from setting the style in the the XAML file that makes use of my control, how can this be accomplished? I'd like to be able to set the style from within the UserControl so that I can see the effects of the style in the designer for the UserControl.

推荐答案

您正试图使一个对是从用户控件的属性(风格在这种情况下),控制内部定义一个ResourceDictionary中。的资源的工作作为一个气泡,如果控制需要一个资源,则它在它的容器的资源中搜索,如果没有找到在那里,然后在容器的resurces的父搜索,如果没有发现它在它的父搜索.... 直到应用程序资源,并且如果不是在系统资源发现

You are trying to make a reference to a ResourceDictionary that is defined inside the control from an user control's property (Style in this case). The resources work as a bubble, if a control need a resources, then it search in its container's resources, if not find there, then search in the parent of the container's resurces, if not find it search in its parent.... until the application resources, and if not found in the system resources.

如果您希望将资源应用到用户控件,那么你应该把它里面的应用程序资源(在App.xaml文件)。试试这个。

If you want to apply a resource to the user control, then you should put it inside the application resources (in the App.xaml file). Try this.

这篇关于设置样式为静态资源为根控制元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆