AttachedProperty 不会传播给孩子?

发布于 2024-09-10 08:05:23 字数 1448 浏览 3 评论 0原文

尝试为 WPF DependencyObject 创建我自己的自定义 AttachedProperty 并没有真正完成我想要它做的事情,而且我有点担心我(再次)没有完全理解 WPF 概念。

我做了一个非常简单的测试类来展示我的问题所在。从 MSDN 文档,我复制了

public class TestBox : TextBox
{
    public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
            "IsBubbleSource",
            typeof(Boolean),
            typeof(TestBox)
            );
    public static void SetIsBubbleSource(UIElement element, Boolean value)
    {
        element.SetValue(IsBubbleSourceProperty, value);
    }
    public static Boolean GetIsBubbleSource(UIElement element)
    {
        return (Boolean)element.GetValue(IsBubbleSourceProperty);
    }
    public Boolean IsBubbleSource
    {
        get
        {
            return (Boolean)GetValue(IsBubbleSourceProperty);
        }
        set
        {
            SetValue(IsBubbleSourceProperty, value);
        }
    }
}

现在,将我的新和时髦的 TextBox 到像这样的网格中,

<Grid vbs:TestBox.IsBubbleSource="true">
    <vbs:TestBox x:Name="Test" Text="Test" >                    
    </vbs:TestBox>
</Grid>

我希望每个没有设置 IsBubbleSource 属性本身的孩子从其父网格“继承”它。它不这样做; MessageBox.Show(Test.IsBubbleSource.ToString()) 显示“false”。附加属性设置为 true。我使用 OnPropertyChanged 事件处理程序检查了这一点。我错过了什么吗?

谢谢!

Trying to create my own custom AttachedProperty for a WPF DependencyObject failed to actually do what I wanted it to do, and I am a bit worried that I (again) did not understand a WPF concept fully.

I made a very simple test class to show where my problem lies. From the MSDN Documentation, I copied

public class TestBox : TextBox
{
    public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
            "IsBubbleSource",
            typeof(Boolean),
            typeof(TestBox)
            );
    public static void SetIsBubbleSource(UIElement element, Boolean value)
    {
        element.SetValue(IsBubbleSourceProperty, value);
    }
    public static Boolean GetIsBubbleSource(UIElement element)
    {
        return (Boolean)element.GetValue(IsBubbleSourceProperty);
    }
    public Boolean IsBubbleSource
    {
        get
        {
            return (Boolean)GetValue(IsBubbleSourceProperty);
        }
        set
        {
            SetValue(IsBubbleSourceProperty, value);
        }
    }
}

Now, placing my new and funky TextBox into a Grid like this

<Grid vbs:TestBox.IsBubbleSource="true">
    <vbs:TestBox x:Name="Test" Text="Test" >                    
    </vbs:TestBox>
</Grid>

I expected every child that does not set the IsBubbleSource property itself to "inherit" it from its parent grid. It does not do this; a MessageBox.Show(Test.IsBubbleSource.ToString()) shows "false". The attached property is set to true. I checked this using an OnPropertyChanged event handler. Did I miss something?

Thanks!

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

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

发布评论

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

评论(1

筱果果 2024-09-17 08:05:23

默认情况下,附加属性不会被继承。您必须在定义属性时指定它:

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
    "IsBubbleSource",
    typeof(Boolean),
    typeof(TestBox),
    new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits)
    );

By default, attached properties are not inherited. You have to specify it when you define the property:

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
    "IsBubbleSource",
    typeof(Boolean),
    typeof(TestBox),
    new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits)
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文