【发布时间】:2011-03-28 11:50:29
【问题描述】:
编辑:我重新表述了整个问题。
大家好,
我有一个带有依赖属性的自定义控件。在 Generic.xaml 文件中,我有一个资源字典。它是外部字典中的资源字典,定义如下:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace">
<!-- This is the dictionary-->
<ResourceDictionary x:Name="TheDictionaryImTalkingAbout" . . . >
.
.
.
</ResourceDictionary>
.
.
.
</ResourceDictionary>
在这个资源字典 TheDictionaryImTalkingAbout 中,我想绑定到我的控件的依赖属性。我尝试了以下 XAML:
<Object x:Key="MyObject" SomeProperty="{Binding MyDependencyProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyNamespace:MyControl}}}"/>
绑定没有返回错误,但是它不起作用。谁能告诉我应该如何从 Generic.xaml 中的资源字典中绑定到我的父控件?
编辑:此绑定确实有效,但仅适用于某些属性。我无法将 GradientStop Color 绑定到颜色类型的依赖属性。当这是一个 UserControl 时它曾经可以工作,但现在我创建了一个自定义控件,它不再工作了。我不知道为什么,但如果你有兴趣,我在这里问了这个问题:
Why can I no longer bind GradientStop Color to a Dependency Property of my control?
【问题讨论】:
标签: c# wpf custom-controls binding dependency-properties