WPF-依赖关系属性错误
-
02-10-2019 - |
题
我正在研究一个WPF项目,我的目的是使两个特定的RadioButtons改变另一个指定组件的属性。但是目前,我只是想在Radiobutton内存储一个字符串。
为此,我创建了一个行为类:
public class AdjustBehavior : Behavior<RadioButton>
{
与此属性:
public static DependencyProperty AdjustLabelContentProperty =
DependencyProperty.RegisterAttached("LabelContent", typeof(String), typeof(AdjustBehavior),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.Inherits));
以及这些获取者和固定器:
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:是互动的命名空间。Behaviorsand I:是调整行为类的名称空间。但是,每当我启动应用程序时,LabelContent都会设置为null。为什么?
我没有发布其余的行为课程,因为我认为这没关系,但是如有必要,我会做。
提前致谢。
克拉克
解决方案
附件的财产需要附加目标。在您的情况下,目标是广播按钮,因此您应该使用
<RadioButton i:AdjustBehavior.LabelContent="Apple" ... />
如果您只需要创建AdwadBehavior的属性,请使用正常的依赖性属性,而不附加。
其他提示
您应该使用depentencyProperty.gregister,而不是注册。这不是用作附件的属性,而是标准的依赖属性。
标记应为Radiobutton上的附加属性,也应是AndiveBehavior上的依赖项属性。
不隶属于 StackOverflow