【问题标题】:How can I bind to dependency properties of a custom control from a resource dictionary defined in Generic.xaml?如何从 Generic.xaml 中定义的资源字典绑定到自定义控件的依赖属性?
【发布时间】: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


    【解决方案1】:

    您不能将数据绑定到 GradientStop,请参阅 How to bind GradientStop Colours or GradientStops Property in Silverlight?

    【讨论】:

    • 这不一定是真的。在我将我的项目从 UserControl 转换为 CustomControl 之前(为了摆脱 XAML 文件),使用上面相同的代码,GradientStop 绑定工作得很好。也许它在 Silverlight 中不起作用,但在 WPF 中起作用,我不知道。
    【解决方案2】:

    我已经从华尔街程序员那里看到了答案。因此,我不知道最终绑定是否会起作用。但是您在绑定中看到的问题是,您必须声明 UserControl 所在的命名空间,然后在绑定中使用它。

    在您的 xaml 之上添加命名空间声明。如果命名空间是“WindowsApplication”,那么这将如下所示:

    xmlns:local="clr-namespace:WindowsApplication"
    

    然后在绑定中,写

    <GradientStop Color="{Binding Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyControl}}}" Offset="0"/>  
    

    【讨论】:

    • 哦,是的,哎呀。谢谢你。我现在可以将我的控件用作祖先类型,但绑定似乎仍然不起作用。这不是因为您不能将数据绑定到 GradientStop,因为正如我在对 Wallstreet Programmer 的评论中所说,它适用于我的控件的 UserControl 版本。我可能会尝试将绑定放在 Generic.xaml 而不是另一个文件中。但我不想那样做。如果您有任何其他解决方案,那就太好了。
    • 您回答了我最初的问题的一部分,所以我继续将您的问题标记为正确的。
    【解决方案3】:

    ResourceDictionary 中的位置与RelativeSource FindAncestor 绑定的解析无关。源在成为可视树的一部分后在运行时解析。您发布的 XAML 中没有任何内容可用于诊断您遇到的问题。

    不相关:是什么导致您选择在另一个 ResourceDictionary 中声明 ResourceDictionary?

    【讨论】:

    【解决方案4】:

    如果有人感兴趣,这就是我如何从我的 Generic.xaml 中绑定到我的依赖项属性以获得自定义控件:

    Generic.xaml 的一部分:

        <ContentControl x:Key="MyFooDepProp" 
                    Content="{TemplateBinding local:MyControl.MyFoo}">
        <!-- ... -->
        </ContentControl>
    
        <!-- ... -->
        <Style TargetType="ContentControl">
            <Setter Property="Content" Value="{TemplateBinding local:MyControl.MyFoo}" />
    
        <!-- ... -->
    

    如果您收到此消息:

    'MyFoo' 成员无效,因为它没有限定类型名称

    关键是使用 TemplateBinding 并在属性前面加上类型名称(就像我做的那样:'local:MyControl.MyFoo')

    【讨论】:

      猜你喜欢
      • 2019-05-26
      • 2012-08-29
      • 2012-08-07
      • 1970-01-01
      • 2011-03-24
      • 2011-06-22
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多