WPF - 依赖属性错误

发布于 2024-09-08 02:09:41 字数 1473 浏览 3 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独﹏钓一江月 2024-09-15 02:09:41

您应该使用 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.

一片旧的回忆 2024-09-15 02:09:41

附加属性需要附加到目标。在您的情况下,目标是单选按钮,
所以你应该使用

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

如果你只需要创建AdjustBehavior的属性,请使用普通的依赖属性,而不是附加的。

Attached property requires target to be attached to. In your case that target is radio button,
so you should use

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

If you need to just create property of AdjustBehavior, use normal dependency property, not attached.

烧了回忆取暖 2024-09-15 02:09:41

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

LabelContent should be either an attached property on RadioButton or dependency property on AdjustBehavior .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文