WPF - 依赖属性错误
我正在开发一个 WPF 项目,我的目的是让两个特定的单选按钮更改另一个指定组件的属性。但现在,我只是想在 RadioButton 中存储一个字符串。
为此,我创建了一个行为类:
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。为什么?
我没有发布我的行为课程的其余部分,因为我认为这并不重要,但如果有必要的话我会做的。
提前致谢。
克拉克
I'm working on a WPF project, and my intention is to make two specific RadioButtons alter properties of another specified Component. But for now, i'm just trying to store a String inside the RadioButton.
For that, I've created a behavior class:
public class AdjustBehavior : Behavior<RadioButton>
{
With this property:
public static DependencyProperty AdjustLabelContentProperty =
DependencyProperty.RegisterAttached("LabelContent", typeof(String), typeof(AdjustBehavior),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.Inherits));
And these getters and setters:
public static String GetLabelContent(RadioButton tb)
{
return (String)tb.GetValue(AdjustLabelContentProperty);
}
public static void SetLabelContent(RadioButton tb, String value)
{
tb.SetValue(AdjustLabelContentProperty, value);
}
On the XAML side, I did this:
<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>
Where int: is the namespace to Interaction.Behaviors and i: is the namespace to the AdjustBehavior class. But whenever I start my application, LabelContent is set to null. Why?
I didn't post the rest of my Behavior class because I think it won't matter, but I'll do if necessary.
Thanks in Advance.
Clark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用 DependencyProperty.Register,而不是 RegisterAttached。这不用作附加属性,而是用作标准依赖属性。
You should use DependencyProperty.Register, not RegisterAttached. This isn't being used as an attached property, but rather a standard dependency property.
附加属性需要附加到目标。在您的情况下,目标是单选按钮,
所以你应该使用
如果你只需要创建AdjustBehavior的属性,请使用普通的依赖属性,而不是附加的。
Attached property requires target to be attached to. In your case that target is radio button,
so you should use
If you need to just create property of AdjustBehavior, use normal dependency property, not attached.
LabelContent 应该是RadioButton 上的附加属性或AdjustBehavior 上的依赖属性。
LabelContent should be either an attached property on RadioButton or dependency property on AdjustBehavior .