使用属性控制装饰器的可见性?

发布于 2024-12-04 23:24:10 字数 1380 浏览 1 评论 0原文

装饰器是否可以根据类中属性的值隐藏/显示?

我应该为此目的使用附加属性吗?

如果是这样,那么如何精确控制装饰器的可见性?我是否必须手动删除它/将其添加到依赖项对象的 OnChanged 事件中的装饰层?

这只是我想要做的事情的一个非常快速的代码表示:(

注意:我什至不确定它是否是正确的做事方式。我希望控制装饰器的可见性由我的业务模型中的代码修改的属性的值。附加属性的问题是控件负责更新属性的值而不是我的业务域中的代码。)

public static class IsValidBehavior
{
    public static readonly DependencyProperty IsValidProperty = DependencyProperty.RegisterAttached("IsValid",
                                                                    typeof(bool),
                                                                    typeof(IsValidBehavior),
                                                                    new UIPropertyMetadata(false, OnIsValidChanged));

    public static bool GetIsValid(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsValidProperty);
    }
    public static void SetIsValid(DependencyObject obj, bool value)
    {
        obj.SetValue(IsValidProperty, value);
    }

    private static void OnIsValidChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        UIElement element = dependencyObject as UIElement;

        if (element == null)
            return;

        if ((bool)e.NewValue == true)
        {
            // Display the Adorner
        }
        else
        {
            // Hide the Adorner
        }
    }
}

Would it be possible for an Adorner to be hidden / displayed depending on the value of a property in a class ?

Should I use attached properties for this purpose ?

If so, how exactly can the Adorner's visibility be controlled; do I have to manually remove it / add it to the Adorner Layer within the Dependency Object's OnChanged event ?

This is just a very quick code representation of what I'm trying to do:

(Note: I'm not even sure if its the right way of doing things. I want the Adorner's visibility to be controlled by the value of a property that is modified by the code in my business model. The problem with Attached Properties is that its the control's responsibility to update the value of the property instead of the code in my business domain.)

public static class IsValidBehavior
{
    public static readonly DependencyProperty IsValidProperty = DependencyProperty.RegisterAttached("IsValid",
                                                                    typeof(bool),
                                                                    typeof(IsValidBehavior),
                                                                    new UIPropertyMetadata(false, OnIsValidChanged));

    public static bool GetIsValid(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsValidProperty);
    }
    public static void SetIsValid(DependencyObject obj, bool value)
    {
        obj.SetValue(IsValidProperty, value);
    }

    private static void OnIsValidChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        UIElement element = dependencyObject as UIElement;

        if (element == null)
            return;

        if ((bool)e.NewValue == true)
        {
            // Display the Adorner
        }
        else
        {
            // Hide the Adorner
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

沉鱼一梦 2024-12-11 23:24:10

好吧,如果我正确理解你的问题,那么在 WPF 中,你有两种方法可以做到这一点,从代码或从 XAML。从代码中,您或多或少已经这样做了,在 XAML 中您可以执行类似的操作,我认为:

Visibility="{Binding Path=MyVisibilityVariant, 
       Converter={StaticResource VisibilityConverter}}

换句话说,将其绑定到某个属性。我的一般建议:尽可能使用 XAML,考虑几种变体:

  • XAML 声明使软件非常可扩展,但也更复杂(考虑您或您的团队的能力,以某种方式在代码后面执行这些操作是最好的,如果不仅解决方案可用)

  • 考虑一下你的截止日期,因为在 XAML 东西上实现/调试/修复你会比在代码上花费更多的时间。

编辑

定义自定义 Adorder 以便能够在 XAML 中定义它

Well, if I right understood your question, in WPF you have 2 ways to do that, from code or from XAML. From code, you more or less already did, in XAML you can do something like this, I think:

Visibility="{Binding Path=MyVisibilityVariant, 
       Converter={StaticResource VisibilityConverter}}

In other words bind it to some property. My general suggestion: is use XAML whenever you can, considering a couple of variants:

  • XAML declaration makes the software very scallable, but also more complex (consider your, or your group cappabilities, somehow doing the stuff in code behind is best, if not only solution available)

  • Consider you deadlines, cause on XAML stuff implementing/debugging/fixing you will spend more time then on code.

EDIT

Defining custom Adorder in order to be able to define it in XAML

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