由于数据绑定,Silverlight中的自定义控件抛出AG_E_PARSER_BAD_PROPERTY_VALUE

时间:2011-07-31 09:15:13

标签: data-binding windows-phone-7

我有2个自定义控件,如下所示:

public class Icon : Control
{
    public Icon()
        : base()
    {
        DefaultStyleKey = typeof(Icon);
    }

    public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(SolidColorBrush), typeof(Icon), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
    public SolidColorBrush Color
    {
        get
        {
            return (SolidColorBrush)GetValue(ColorProperty);
        }
        set
        {
            SetValue(ColorProperty, value);
        }
    }
}

public class Rating : Control
{
    private StackPanel _panel;

    public Rating()
        : base()
    {
        DefaultStyleKey = typeof(Rating);
    }

    public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("ColorProperty", typeof(SolidColorBrush), typeof(Rating), null);
    public SolidColorBrush Color
    {
        get
        {
            return (SolidColorBrush)GetValue(ColorProperty);
        }
        set
        {
            SetValue(ColorProperty, value);
            Debug.WriteLine(Name + " - Set Color");
        }
    }

    public override void OnApplyTemplate()
    {
        Debug.WriteLine(Name + " - On Apply Template");
        base.OnApplyTemplate();
    }
}

这些控件的UI在Generic.xaml中定义:

<Style TargetType="lib:Icon">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="lib:Icon">
                <Ellipse Width="32" Height="32" Margin="4" Fill="{Binding Path=Color, RelativeSource={RelativeSource TemplatedParent}}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="lib:Rating">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="lib:Rating">
                <StackPanel x:Name="Panel" Orientation="Horizontal">
                    <lib:Icon Color="{Binding Path=Color, RelativeSource={RelativeSource TemplatedParent}}"/>
                    <lib:Icon Color="{Binding Path=Color, RelativeSource={RelativeSource TemplatedParent}}"/>
                    <lib:Icon Color="{Binding Path=Color, RelativeSource={RelativeSource TemplatedParent}}"/>
                    <lib:Icon Color="{Binding Path=Color, RelativeSource={RelativeSource TemplatedParent}}"/>
                    <lib:Icon Color="{Binding Path=Color, RelativeSource={RelativeSource TemplatedParent}}"/>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在在我的MainPage.xaml中,我使用Rating控件,如下所示:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal" Grid.Row="0">
        <Button Content="Yellow" Click="ChangeDataContext"/>
    </StackPanel>
    <StackPanel Grid.Row="1">
        <lib:Rating Color="Red"/>
        <lib:Rating Name="MyRating" Color="{Binding Path=Color}"/>
    </StackPanel>
</Grid>

在代码隐藏中,我按如下方式设置数据上下文:

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        ColorData data = new ColorData() { Color = new SolidColorBrush(Colors.Cyan) };
        MyRating.DataContext = data;
    }

    private void ChangeDataContext(object sender, System.Windows.RoutedEventArgs e)
    {
        ColorData data = new ColorData() { Color = new SolidColorBrush(Colors.Yellow) };
        MyRating.DataContext = data;
    }
}

public class ColorData
{
    public SolidColorBrush Color { get; set; }
}

当我运行此代码时,我收到指向MainPage.XAML中数据绑定行的AG_E_PARSER_BAD_PROPERTY_VALUE。 有人会知道为什么会这样,以及我如何解决这个问题?

由于

2 个答案:

答案 0 :(得分:0)

CLR Property getter / setter不用于DataBinding。您的评级控制缺乏以下内容:

public static Color GetColor(DependencyObject o)
{
    return (Color) o.GetValue(ColorProperty);
}

public static void SetColor(DependencyObject o, Color value)
{
    o.SetValue(ColorProperty, value);
}

答案 1 :(得分:0)

您为Color属性注册了错误的名称。变化

public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("ColorProperty", typeof(SolidColorBrush), typeof(Rating), null);

public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(SolidColorBrush), typeof(Rating), null);

它应该有用。