首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么不能再将GradientStop颜色绑定到控件的依赖属性?

为什么不能再将GradientStop颜色绑定到控件的依赖属性?
EN

Stack Overflow用户
提问于 2010-08-13 07:54:28
回答 1查看 1.6K关注 0票数 0

我的问题总结如下:我创建了一个UserControl,将我的依赖属性“方案”绑定到GradientStop的颜色属性,它工作得很好。现在,在将我的UserControl转换为自定义控件后,这个绑定不再有效,我不知道为什么。

这就是我在UserControl1.xaml文件中为UserControl声明资源字典的方式。

UserControl1.xaml

代码语言:javascript
复制
<!-- Resources -->
<UserControl.Resources>
  <ResourceDictionary>
   <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Colors.xaml"/>
    <ResourceDictionary Source="Styles.xaml"/>
   </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</UserControl.Resources>

Colors.xaml中,我有以下内容:

Colors.xaml

代码语言:javascript
复制
<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}"/>

依赖属性方案绑定到GradientStop的颜色属性工作得很好,就像我想要的那样。LinearGradientBrush使用方案颜色来创建一个渐变,从方案的颜色到方案的稍微暗一点的阴影。

UserControl中有一个使用colorBrush作为背景的矩形。这一切都很完美,我喜欢它。

现在,环境使我将这个UserControl转换为CustomControl。为此,我将我的UserControl的XAML文件复制到控件模板中的Generic.xaml中。

在尝试了不同的方法之后,似乎可以很好地在Generic.xaml中声明我的资源字典

Generic.xaml

代码语言:javascript
复制
 <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文件中,我有以下内容:

在我的自定义控件的Colors.xaml中:

代码语言:javascript
复制
<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}"/>

现在,由于一些我无法理解的令人沮丧的原因,到GradientStop的绑定不再起作用。我组织资源的方式没有任何问题,因为我制作的调试文本块完美地绑定到了Title依赖属性。

GradientStop颜色没有绑定到方案依赖属性,这让我很恼火。

我怎么才能修复它?

Dictionary1.xaml中,使用x:key "text2"查看文本块。colorBrush使用来自colorSchemeGradient的渐变停靠点,该方案使用绑定到方案。然而,该绑定失败了,因此什么都不起作用。如果你能让这个绑定工作起来,那你就太棒了。

为什么绑定在这里起作用,而在自定义控件中不起作用?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-13 13:07:15

在您的测试项目中,您似乎在需要静态引用的地方使用了DynamicResources,在不需要的地方使用了转发引用。

在Dictionary1.xaml中,将内容更改为:

代码语言:javascript
复制
<!-- 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中的text1和text2引用。

注意一些地方的StaticResource而不是DynamicResource,以及删除前向引用的重新排序。如果您阅读http://msdn.microsoft.com/en-us/library/ms750613.aspx,您可能会注意到动态资源查找行为与静态资源查找行为有很大的不同。

请注意,切换到静态并不会阻止控件响应对Scheme属性的更改-资源的查找是静态的,而不是那些资源中属性的值。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3472932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档