2011-07-31 59 views
0

AG_E_PARSER_BAD_PROPERTY_VALUE我有2個定製控件定義如下:自定義控制在Silverlight投擲由於數據綁定

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; } 
} 

當運行該代碼時,收到AG_E_PARSER_BAD_PROPERTY_VALUE指向在MainPage.xaml數據綁定線。 會有人知道爲什麼這是如此,我怎麼能解決這個問題?

謝謝

回答

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

,它應該工作。

+0

當然:)我試圖寫一個示例問題,並以某種方式在註冊過程中錯誤地使用Color - > ColorProperty犯了一個複製粘貼錯誤。 –

0

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); 
} 
+0

我的錯誤是在依賴註冊過程中。您的上述建議是否與ColorData類相關? –

相關問題