为什么我不再绑定渐变停止颜色以我的控制依赖属性? [英] Why can I no longer bind GradientStop Color to a Dependency Property of my control?

查看:184
本文介绍了为什么我不再绑定渐变停止颜色以我的控制依赖属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题的总结:我创造,我必将我的依赖属性计划,以一个渐变停止的色彩属性用户控件,它完美地工作。现在,我的用户转换为自定义的控制后,此绑定不再起作用了,我不知道为什么。

The summary of my problem: I had created a UserControl where I bound my dependency property "Scheme" to the Color property of a GradientStop, and it worked perfectly. Now, after converting my UserControl to a custom control, this binding no longer works, and I don't know why.

编辑:
自定义控制问题的复制: http://www.megaupload.com/?d=0006XVYD

编辑:
一个等价用户控制,其中绑定工作: http://www.megaupload.com/?d=W13GTD4E

这是我宣布我的字典资源为我在UserControl1.xaml文件用户控件。

This is how I declared my resource dictionaries for my UserControl in the UserControl1.xaml file.

在UserControl1.xaml

<!-- Resources -->
<UserControl.Resources>
  <ResourceDictionary>
   <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Colors.xaml"/>
    <ResourceDictionary Source="Styles.xaml"/>
   </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</UserControl.Resources>

在Colors.xaml,我有以下几点:

Within Colors.xaml, I have the following:

在Colors.xaml

<GradientStopCollection x:Key="colorSchemeGradient">
 <GradientStop Color="{Binding Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Offset="0"/>
 <GradientStop Color="#000000" Offset="3"/>
</GradientStopCollection>

<LinearGradientBrush x:Key="colorBrush" StartPoint="0,0" EndPoint="0,1" GradientStops="{DynamicResource colorSchemeGradient}"/>

依赖属性计划的结合渐变停止的色彩特性完美的作品,到底我想它。在一个LinearGradientBrush使用计划色彩来营造一个渐变,从方案的颜色它的一个稍微暗的阴影去。用户控件中有一个使用colorBrush作为其背景的矩形。这一切都完美的作品,我喜欢它。

The dependency property Scheme's binding to the color property of the GradientStop WORKS PERFECTLY, exactly how I want it to. The LinearGradientBrush uses the Scheme color to create a gradient that goes from the color of scheme to a slightly darker shade of it. The UserControl has a rectangle in it that uses the colorBrush as its background. It all works perfectly, and I love it.

现在,情况正在我这个用户控件转换为CustomControl。我采取我的用户和pretty的XAML文件复制得多及其所有内容到Generic.xaml在控件模板这样做。尝试不同的东西出来后,这似乎工作pretty以及在Generic.xaml宣布我的资源字典:

Now, circumstances are making me convert this UserControl to a CustomControl. I'm doing this by taking the XAML file of my UserControl and pretty much copying all its content into Generic.xaml in the control template. After trying different things out, this has seemed to work pretty well for declaring my resource dictionaries in Generic.xaml:

在Generic.xaml

 <Style TargetType="{x:Type local:MyCustomControl}">

   <!-- Style Resources -->
   <Style.Resources>

   <!-- Resource Dictionaries -->
   <ResourceDictionary>
     <ResourceDictionary.MergedDictionaries>
       <ResourceDictionary Source="/MyCustomControl;component/themes/Colors.xaml"/>
       <ResourceDictionary Source="/MyCustomControl;component/themes/Styles.xaml"/>
     </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>

   </Style.Resources>

            .
            .
            .
 </Style>

现在,在我的Colors.xaml文件,我有以下几点:

Now, in my Colors.xaml file, I have the following:

在Colors.xaml为我的自定义控件

<GradientStopCollection x:Key="colorSchemeGradient">
 <GradientStop Color="{Binding Path=Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType=l:MyCustomControl}}" Offset="0"/>
 <GradientStop Color="#000000" Offset="3"/>
</GradientStopCollection>


<!-- FOR DEBUG PURPOSES -->
<TextBlock x:Key="whoaText" Text="{Binding Path=Title, RelativeSource={RelativeSource FindAncestor, AncestorType=l:MyCustomControl}}"/>

<LinearGradientBrush x:Key="colorBrush" StartPoint="0,0" EndPoint="0,1" GradientStops="{DynamicResource colorSchemeGradient}"/>

现在,一些非常令人沮丧的原因,我无法理解,要渐变停止绑定不工作了。还有如何我安排我的资源没有问题,因为调试文本块我做了绑定到标题依赖属性完美。渐变停止颜色不绑定到该计划依赖属性,它是讨厌我。

Now, for some very frustrating reason I cannot understand, the binding to the GradientStop doesn't work anymore. There are no problems in how I have organized my resources, because that debug textblock I made binds to the Title dependency property perfectly. The GradientStop color does not bind to the Scheme dependency property and it is annoying me.

谁能告诉我这是怎么回事,我该如何解决这个问题?

Can anyone tell me what's going on and how I can fix it?

编辑:我已经在测试项目中重复我的问题: http://www.megaupload.com / D = 0006XVYD

I have replicated my problem in a test project: http://www.megaupload.com/?d=0006XVYD

在Dictionary1.xaml看与x的文本块:键文本2。所述colorBrush使用来自colorSchemeGradient,它使用一个结合方案梯度停止。但是,绑定失败,因此没有什么作品。如果你能得到该绑定工作,你真棒。

In Dictionary1.xaml look at the textblock with the x:key "text2". The colorBrush uses gradient stops from colorSchemeGradient, which uses a binding to Scheme. But that binding fails and so nothing works. If you can get that binding working, you're awesome.

编辑:下面是一个等价的用户控件,其中结合的作品:
http://www.megaupload.com/?d=W13GTD4E

Here's an equivalent user control where the binding works: http://www.megaupload.com/?d=W13GTD4E

为什么这里,而不是做装订工作的自定义控件?

Why does the binding work here and not in the custom control?

非常感谢你,

达拉尔

推荐答案

在您的测试项目在的http:// megaupload.com/?d=0006XVYD ,你似乎有你需要的静态DynamicResources,并在不需要向前引用。

In your test project at http://megaupload.com/?d=0006XVYD , You seem to have DynamicResources where you need static, and forward references where not required.

在Dictionary1.xaml,内容更改为:

In Dictionary1.xaml, change the contents to:

<!-- Color Scheme Gradient: Used in the label background and border brush.
                                SUBTLE gradient from the colorScheme color
                                to a slightly darker shade -->
    <GradientStopCollection x:Key="colorSchemeGradient">
        <GradientStop Color="{Binding Path=Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType=local:CustomControl1}}" Offset="0"/>
        <GradientStop Color="#000000" Offset="3"/>
    </GradientStopCollection>

    <LinearGradientBrush x:Key="colorBrush" GradientStops="{StaticResource colorSchemeGradient}"/>

    <TextBlock x:Key="text1" Text="{Binding Path=Name, RelativeSource={RelativeSource FindAncestor, AncestorType=local:CustomControl1}}" Foreground="Blue"/>
    <TextBlock x:Key="text2" Text="This text doesn't show!  Grr..." Foreground="{StaticResource colorBrush}"/>

我也Generic.xaml改变了文本1和文本引用。

I also changed the text1 and text2 references in Generic.xaml.

请注意在静态资源,而不是 DynamicResource 在一些地方,以及重新排序,以删除向前引用。如果你读 http://msdn.microsoft.com/en-us/library/ ms750613.aspx ,你可能会注意到,dynamicresource查找行为是比静态的好有点不同。

Note the StaticResource rather than DynamicResource in a few places, as well as the reorder to remove forward references. If you read http://msdn.microsoft.com/en-us/library/ms750613.aspx , you may note that dynamicresource lookup behavior is a good bit different than static.

请注意,切换到静态不会从响应保持控制,以改变计划属性 - 在资源的查找的是静态的,在这些资源属性不是值

Note that switching to static does NOT keep the control from responding to changes to the Scheme property - the lookup of the resources is static, not the value of properties in those resources.

这篇关于为什么我不再绑定渐变停止颜色以我的控制依赖属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆