WPF:依赖属性和放大器;资源 [英] WPF: Dependency Properties & Resources
问题描述
我发现这个MSDN上:
I found this on MSDN:
一个依赖属性值可以通过引用资源来设置。资源通常被指定为资源
属性页根元素,或应用程序的价值(这些位置实现最便捷的资源)。下面的示例演示如何定义一个的SolidColorBrush
的资源。
A dependency property value can be set by referencing a resource. Resources are typically specified as the Resources
property value of a page root element, or of the application (these locations enable the most convenient access to the resource). The following example shows how to define a SolidColorBrush
resource.
XAML:
<DockPanel.Resources>
<SolidColorBrush x:Key="MyBrush" Color="Gold"/>
</DockPanel.Resources>
在该资源被定义,可以参考该资源,并用它来提供一个属性值:
Once the resource is defined, you can reference the resource and use it to provide a property value:
<Button Background="{DynamicResource MyBrush}" Content="I am gold" />
这特定的资源被引用为一个DynamicResource标记扩展。 应用于使用动态资源引用,您必须设置一个依赖属性,所以它是专门由WPF属性系统启用了动态资源参考使用。的
This particular resource is referenced as a DynamicResource Markup Extension . To use a dynamic resource reference, you must be setting to a dependency property, so it is specifically the dynamic resource reference usage that is enabled by the WPF property system.
我的问题是:
- 的StaticResource不被视为依赖项属性?如果是,为什么?
- 在不属于WPF属性系统?
另外你能不能给我一个例子,如何使用依赖项属性来实现默认值?
Also can you give me an example how to implement default value using Dependency Property?
推荐答案
DynamicResource 的用于设置的仅依赖属性值。
相反的的StaticResource 的可pratically使用无处不在。你可以用它来设置一个依赖属性值,但不仅。例如,您也可以定义元素的资源作为使用在面板内部通过的的StaticResource 的,比如在code
By contrast StaticResource can be used pratically everywhere. You can use it to set a dependency property value but not only. For example, you can also define an element as resource as use it inside a panel by StaticResource, such as in the code
<Window>
<Window.Resources>
<Button Content="btnStaticResource" x:Key="myBtn" />
</Window.Resources>
<Grid>
<StaticResource ResourceKey="myBtn" />
</Grid>
</Window>
关于您的问题1 ,资源不是一个依赖项属性,不论你使用的的StaticResource 或 DynamicResource 的标记扩展参考。
Concerning your question 1, a resource is not a dependency property, irrespective if you refer to it using StaticResource or DynamicResource markup extension.
在WPF中的资源可以是任何东西,.NET对象,字体,图像,颜色,字符串等。 资源的概念是不相关的依赖项属性的概念
A resource in WPF can be about anything, a .NET object, a font, an image, a color, a string etc. The concept of resource is not related to the concept of dependency property.
一个依赖项属性是财产WPF引入了一个新类型。按照固定hierarchie依赖属性值取决于多个源(详情 MSDN 一>)。
A dependency property is a new type of property introduced by WPF. A dependency property value depends on multiple sources according to a fixed hierarchie (for details msdn).
关于您的问题2 ,是的,的StaticResource的概念是WPF资源系统的组成部分。
Concerning your question 2, yes, the concept of StaticResource is part of the WPF resource system.
最后,定义依赖属性的默认值,请参见下面的code:
Finally, for defining the default value of a dependency property see the following code:
public static readonly DependencyProperty AlphaProperty = DependencyProperty.Register ("Alpha", typeof(int), typeof(MyButton), new FrameworkPropertyMetadata(255, FrameworkPropertyMetadataOptions.AffectsRender));
下面的定义int类型,并与缺省值为255名为阿尔法一个依赖属性。
Here is defined a dependency property named Alpha, of type int and with default value 255.
我希望这有助于
这篇关于WPF:依赖属性和放大器;资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!