【问题标题】:Databinding the color of a RadialGradient brush in silverlight 3在 silverlight 3 中对 RadialGradient 笔刷的颜色进行数据绑定
【发布时间】:2009-08-03 04:44:53
【问题描述】:

我正在尝试将 silverlight 3 中 RadialGradientBrush 的颜色数据绑定到属性,但似乎无法使其正常工作。

例如,在一个示例测试应用程序中,我只有

<navigation:Page x:Class="SilverlightNavigator.HomePage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
x:Name="HomePageUC"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
       Title="HomePage Page">
<Grid x:Name="LayoutRoot" Background="White">
        <StackPanel>

            <TextBlock 
                       DataContext="{Binding ElementName=HomePageUC}" 
                        Text="{Binding TestColorOne}" />

        <Rectangle x:Name="testRectangle" Height="100" Width="100" 
                   DataContext="{Binding ElementName=HomePageUC}" >
                <Rectangle.Fill>
                <RadialGradientBrush>
                    <GradientStop Color="{Binding TestColorOne}" Offset="0" />
                    <GradientStop Color="{Binding TestColorTwo}" Offset="1"/>
                    <!--
                    <GradientStop Color="#FFFF0000" />
                    <GradientStop Color="#FF00FF00" Offset="1"/>
                    -->
                </RadialGradientBrush>
                </Rectangle.Fill>
            </Rectangle>

        </StackPanel>

在后面的代码中,我什至让它们像这样的依赖属性..

    public static readonly DependencyProperty TestColorOneProperty =
        DependencyProperty.RegisterAttached("TestColorOne", typeof(Color), typeof(HomePage), null);

    public static readonly DependencyProperty TestColorTwoProperty =
        DependencyProperty.RegisterAttached("TestColorTwo", typeof(Color), typeof(HomePage), null);


    public Color TestColorOne
    {
        get { return (Color)GetValue(TestColorOneProperty); }
        set { SetValue(TestColorOneProperty, value); }
    }

    public Color TestColorTwo
    {
        get { return (Color)GetValue(TestColorTwoProperty); }
        set { SetValue(TestColorTwoProperty, value); }
    }

但这仍然给了我非常无用的 AG_E_PARSER_BAD_PROPERTY_VALUE 异常。如果我取消注释颜色是硬编码的两条 Xaml 行,它工作正常。我知道属性很好,因为如果我硬编码颜色或注释掉矩形,它会很好地显示文本。 (通过绑定到 TextBlock)

我也尝试过传入字符串“Red”、“Blue”等而不是颜色对象。但是绑定似乎不起作用。

有什么建议吗?

【问题讨论】:

    标签: .net silverlight data-binding xaml


    【解决方案1】:

    不幸的是,您不能这样做,因为绑定发生在 FrameworkElement 和继承它的每个对象上。 GradientStop 不是 FrameworkElement,它会阻止您绑定其 Color 属性。

    【讨论】:

    • 感谢基里尔的回复。那么有解决方法吗?例如使用数据/事件触发器。
    【解决方案2】:

    好的,我通过将 RadialGradientBrush 本身上一层并数据绑定来解决了这个问题。

            <Rectangle x:Name="testRectangle" Height="100" Width="100" 
                       Fill="{Binding TestRadialGradientBrush}"
                       DataContext="{Binding ElementName=HomePageUC}" >
                </Rectangle>
    

    RadialGradientBrush 依次实例化,将 GradientStops 设置为我在后面的代码中想要的颜色。

    【讨论】:

    • 你能把TestRadialGradientBrush的代码放在后面吗?谢谢
    • 这是很久以前的事了。不确定是否有任何代码是相关的,是否有效,甚至是否可以编译。但是你可以从这里下载整个源代码chaitanyaonline.net/2009/08/24/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 2015-03-07
    相关资源
    最近更新 更多