简体   繁体   English

如何从 Generic.xaml 中定义的资源字典绑定到自定义控件的依赖属性?

[英]How can I bind to dependency properties of a custom control from a resource dictionary defined in Generic.xaml?

EDIT: I rephrased the entire question.编辑:我改写了整个问题。

Hello everybody,大家好,

I have a custom control with dependency properties.我有一个具有依赖属性的自定义控件。 In the Generic.xaml file I have a resource dictionary.在 Generic.xaml 文件中,我有一个资源字典。 It's a resource dictionary within the outer dictionary, defined like so:它是外部字典中的资源字典,定义如下:

<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>

In this resource dictionary, TheDictionaryImTalkingAbout, I want to bind to a dependency property of my control.在这个资源字典 TheDictionaryImTalkingAbout 中,我想绑定到我的控件的依赖属性。 I tried the following XAML :我尝试了以下 XAML:

<Object x:Key="MyObject" SomeProperty="{Binding MyDependencyProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyNamespace:MyControl}}}"/>

The binding returns no errors, however, it doesn't work.绑定不会返回任何错误,但是,它不起作用。 Can anyone tell me how I'm supposed to bind to my parent control from within a resource dictionary that's within Generic.xaml?谁能告诉我应该如何从 Generic.xaml 中的资源字典中绑定到我的父控件?

EDIT: This binding DOES work, but only for certain properties.编辑:此绑定确实有效,但仅适用于某些属性。 I am unable to bind GradientStop Color to a dependency property of type color.我无法将 GradientStop Color 绑定到颜色类型的依赖属性。 It USED to work when this was a UserControl, but it doesn't work anymore now that I created a custom control.当这是一个 UserControl 时它曾经可以工作,但是现在我创建了一个自定义控件它不再工作了。 I don't know why, but if you're interested, I asked this question here:我不知道为什么,但如果你有兴趣,我在这里问了这个问题:

Why can I no longer bind GradientStop Color to a Dependency Property of my control? 为什么我不能再将 GradientStop Color 绑定到我的控件的依赖属性?

Location in a ResourceDictionary has nothing to do with resolution of a RelativeSource FindAncestor Binding. ResourceDictionary 中的位置与 RelativeSource FindAncestor Binding 的解析无关。 The Source is resolved at runtime after it becomes part of a Visual Tree.源在成为可视化树的一部分后在运行时解析。 There is nothing in the XAML you have posted that could be used to diagnose the problem you are having.您发布的 XAML 中没有任何内容可用于诊断您遇到的问题。

Unrelated: What led you choose to declare a ResourceDictionary inside another ResourceDictionary?无关:是什么导致您选择在另一个 ResourceDictionary 中声明 ResourceDictionary?

If anyone is interested, this is how I bind to my dependency property from inside my Generic.xaml for a custom control:如果有人感兴趣,这就是我从 Generic.xaml 内部绑定到我的依赖属性以获取自定义控件的方式:

A part from 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}" />

    <!-- ... -->

If you receive this message:如果您收到此消息:

'MyFoo' member is not valid because it does not have a qualifying type name “MyFoo”成员无效,因为它没有合格的类型名称

The key is to use TemplateBinding and prefix your property with the type name (like I did: 'local:MyControl.MyFoo')关键是使用 TemplateBinding 并为您的属性添加类型名称的前缀(就像我所做的:'local:MyControl.MyFoo')

您无法将数据绑定到 GradientStop,请参阅如何在 Silverlight 中绑定 GradientStop 颜色或 GradientStops 属性?

I have seen the answer from Wallstreet Programmer.我从华尔街程序员那里看到了答案。 Therefore, I don't know if in the end, the binding will work.因此,我不知道最终绑定是否有效。 But the problem that you see with your binding is, that you must declare the namespace where your UserControl is and then use this in the binding.但是您在绑定中看到的问题是,您必须声明 UserControl 所在的命名空间,然后在绑定中使用它。

Add a namespace-declaration on top of your xaml.在 xaml 之上添加命名空间声明。 If the namespace is "WindowsApplication" then this will look like follows:如果命名空间是“WindowsApplication”,那么这将如下所示:

xmlns:local="clr-namespace:WindowsApplication"

Then in the binding, write然后在绑定中,写

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义控件和generic.xaml中的其他样式 - Custom control and additional style in generic.xaml for it 以编程方式从Generic.xaml查找资源 - Find resource from Generic.xaml programmatically 如何在Generic.xaml中获取应用程序资源 - How to get application resource in Generic.xaml 覆盖同一解决方案中另一个项目的Generic.xaml资源字典资源 - Override Generic.xaml Resource Dictionary resources from another project in same solution 自定义控件样式不起作用(通过Generic.xaml) - Custom control styles not working (via Generic.xaml) 绑定Generic.xaml中的画布背景以进行自定义控件 - Binding Canvas Background in Generic.xaml for custom control 如何使用“ Themes \\ Generic.xaml”应用自定义控件的默认模板? - How is the default template of a custom control applied with out using “Themes\Generic.xaml ?” 在Generic.xaml的模板化控件中应用默认样式 - Applying default style in templated control from Generic.xaml 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml? generic.xaml中的数据模板如何自动应用? - How can data templates in generic.xaml get applied automatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM