我正在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。为什么?
我没有发布我的行为类的其余部分,因为我认为它没有关系,但我会做必要的。
在此先感谢。
克拉克
有道理,非常感谢!现在正在工作 – 2010-07-01 20:48:00