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

发布于 2024-09-13 12:21:11 字数 1172 浏览 4 评论 0原文

编辑:我重新表述了整个问题。

大家好,

我有一个带有依赖属性的自定义控件。在 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 绑定到颜色类型的依赖属性。当这是一个用户控件时它曾经可以工作,但现在我创建了一个自定义控件,它不再工作了。我不知道为什么,但如果您感兴趣,我在这里问这个问题:

为什么我不能再将 GradientStop Color 绑定到控件的依赖属性?

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. 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. I tried the following 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?

EDIT: This binding DOES work, but only for certain properties. I am unable to bind GradientStop Color to a dependency property of type color. It USED to work when this was a UserControl, but it doesn't work anymore now that I created a custom control. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森林迷了鹿 2024-09-20 12:21:11

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

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

Location in a ResourceDictionary has nothing to do with resolution of a 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.

Unrelated: What led you choose to declare a ResourceDictionary inside another ResourceDictionary?

只为守护你 2024-09-20 12:21:11

我看到了华尔街程序员的回答。因此,我不知道最终绑定是否有效。但是您在绑定中看到的问题是,您必须声明 UserControl 所在的命名空间,然后在绑定中使用它。

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

xmlns:local="clr-namespace:WindowsApplication"

然后在绑定中写入

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

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.

Add a namespace-declaration on top of your xaml. If the namespace is "WindowsApplication" then this will look like follows:

xmlns:local="clr-namespace:WindowsApplication"

Then in the binding, write

<GradientStop Color="{Binding Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyControl}}}" Offset="0"/>  
漫雪独思 2024-09-20 12:21:11

如果有人感兴趣,这就是我从 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')

If anyone is interested, this is how I bind to my dependency property from inside my Generic.xaml for a custom control:

A part from 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

The key is to use TemplateBinding and prefix your property with the type name (like I did: 'local:MyControl.MyFoo')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文