【问题标题】:Custom Control in Silverlight throwing AG_E_PARSER_BAD_PROPERTY_VALUE because of Data-Binding由于数据绑定,Silverlight 中的自定义控件抛出 AG_E_PARSER_BAD_PROPERTY_VALUE
【发布时间】:2011-07-31 09:15:13
【问题描述】:

我有 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。 有人知道为什么会这样以及我该如何解决吗?

谢谢

【问题讨论】:

    标签: data-binding windows-phone-7


    【解决方案1】:

    您为 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);
    

    它应该可以工作。

    【讨论】:

    • 当然 :) 我试图写一个示例问题,但在注册过程中不知何故犯了一个错误,即颜色 -> ColorProperty 的复制粘贴错误。
    【解决方案2】:

    CLR 属性 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);
    }
    

    【讨论】:

    • 我的错误是在依赖注册过程中。您的上述建议是否与 ColorData 类有关?
    猜你喜欢
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 2011-10-13
    • 2013-02-21
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    相关资源
    最近更新 更多