0

我正在WPF项目上工作,我的意图是让两个特定的RadioButton改变另一个指定组件的属性。但现在,我只是想在RadioButton中存储一个String。WPF - 依赖属性错误

对于这一点,我创建了一个行为类:

public class AdjustBehavior : Behavior<RadioButton> 
{ 

使用这个属性:

 public static DependencyProperty AdjustLabelContentProperty = 
     DependencyProperty.RegisterAttached("LabelContent", typeof(String), typeof(AdjustBehavior), 
      new FrameworkPropertyMetadata(null, 
    FrameworkPropertyMetadataOptions.Inherits)); 

而且这些getter和setter方法:

 public static String GetLabelContent(RadioButton tb) 
    { 
     return (String)tb.GetValue(AdjustLabelContentProperty); 
    } 

    public static void SetLabelContent(RadioButton tb, String value) 
    { 
     tb.SetValue(AdjustLabelContentProperty, value); 
    } 

在XAML侧,我这样做:

<RadioButton Content="Banana" Height="16" HorizontalAlignment="Left" Margin="30,216,0,0" Name="radioButton1" VerticalAlignment="Top" GroupName="a" IsThreeState="False" IsChecked="True" Checked="radioButton1_Checked" > 
     <int:Interaction.Behaviors> 
      <i:AdjustBehavior LabelContent="Apple" /> 
     </int:Interaction.Behaviors> 
    </RadioButton> 

其中int:是Interaction.Behaviors的命名空间,i:是AdjustBehavior类的命名空间。但是,每当我开始我的应用程序,LabelContent被设置为null。为什么?

我没有发布我的行为类的其余部分,因为我认为它没有关系,但我会做必要的。

在此先感谢。

克拉克

回答

0

附加属性需要附加目标。你的情况是目标单选按钮, 所以如果你只是需要创建AdjustBehavior的财产,用一般的依赖属性,没有连接你应该使用

<RadioButton i:AdjustBehavior.LabelContent="Apple" ... /> 

+0

有道理,非常感谢!现在正在工作 – 2010-07-01 20:48:00

1

你应该使用DependencyProperty.Register,不RegisterAttached。这不是用作附加属性,而是用作标准依赖属性。

+0

非常感谢,解决了我的问题 – 2010-07-01 20:47:42

0

LabelContent应该是RadioButton上的附加属性或AdjustBehavior上的依赖项属性。