WPF Getter / Setter未被调用 [英] WPF Getter/Setter Not Being Called

查看:62
本文介绍了WPF Getter / Setter未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码。正在调用PropertyChanged事件,但不调用getter和setter。我一辈子都看不到为什么。我还有其他属性,其中发生相同的事情,但正在设置值。如果这有所不同,这是一个自定义控件。当我在get / set处设置断点时,我什么也没得到。但是,更改的属性返回正确的值。

I have the following code. The PropertyChanged event is being called but the getter and setter is not. I can't for the life of me see why not. I have other properties where the same thing is happening but the values are being set. This is a custom control, if that makes a difference. When I put a breakpoint at the get/set I get nothing. However, the propertychanged is returning the correct value. Any help is greatly appreciated.

    /// <summary>
    /// Identifies the PlacementTarget dependency property.
    /// </summary>
    public static readonly DependencyProperty PlacementTargetProperty =
        DependencyProperty.RegisterAttached(
            "PlacementTarget",
            typeof(UIElement),
            typeof(FunctionPanel),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, PlacementTargetPropertyChanged)
        );

    /// <summary>
    /// Gets or setsthe PlacementTarget of tooltips for all child controls.
    /// </summary>
    public UIElement PlacementTarget
    {
        get { return (UIElement)GetValue(PlacementTargetProperty); }
        set { SetValue(PlacementTargetProperty, value); }
    }

    /// <summary>
    /// Sets the value of the PlacementTarget dependency property.
    /// </summary>
    /// <param name="target">Target object.</param>
    /// <param name="value">Value argument.</param>
    public static void SetPlacementTarget(DependencyObject target, UIElement value)
    {
        target.SetValue(PlacementTargetProperty, value);
    }

    /// <summary>
    /// Gets the value of the PlacementTarget dependency property.
    /// </summary>
    /// <param name="target">Target object.</param>
    public static UIElement GetPlacementTarget(DependencyObject target)
    {
        return (UIElement)target.GetValue(PlacementTargetProperty);
    }

    /// <summary>
    /// This method is called when the PlacementTarget dependency property changes value.
    /// </summary>
    /// <param name="sender">Sender object.</param>
    /// <param name="e">Event arguments.</param>
    private static void PlacementTargetPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        ToolTipService.SetPlacementTarget(sender, (UIElement)e.NewValue);
    }

其他信息

因此本质上PlacementTargetPropertyChanged可以很好地处理它。我想问题是这个特定的DP实际上并没有向孩子们传播。这是XAML代码段:

So essentially PlacementTargetPropertyChanged is handling it fine. I guess the issue is that this particular DP isn't actually propagating to the children. Here's the XAML snippet:

<controls:FunctionPanel BetweenShowDelay="0"
                        InitialShowDelay="500"
                        IsError="False"
                        Message="MESSAGE"
                        Placement="Right"
                        PlacementTarget="{Binding RelativeSource={RelativeSource Self}}"
                        ShowDuration="60000" />

除PlacementTarget之外的所有DP都进入子元素。我尝试删除绑定,但没有任何作用。

All of the DPs except PlacementTarget are getting to the children elements. I've tried removing the binding but it made no difference.

我正在使用Snoop检查值来验证其他DP。我创建了一个Int类型的InitialShowDelay DP。当我将值设置为5000时,所有子控件都将继承该值,并且工具提示会在显示前延迟5秒。我可以通过在我的PlacementTargetPropertyChanged方法上设置一个断点来验证绑定是否发送了正确的控件。

I'm verifying the other DPs by using Snoop to check the values. I have created a InitialShowDelay DP that is of type Int. When I set the value to 5000, all child controls inherit that and tooltips delay 5 seconds before appearing. I can verify that the binding is sending the correct control by putting a breakpoint on my PlacementTargetPropertyChanged method.

所以PlacementTarget似乎是工作正常。我的展示位置附加属性的默认值(设置为正确)将被忽略。当我通过XAML进行设置时,它仍然无法正常工作。但是,如果将值设置为顶部,左侧或底部,则工具提示将显示在正确的位置。

So it seems that PlacementTarget is working fine. The default value of my Placement attached property, which is set to "right", is being ignored. When I set this through XAML, it still doesn't work. However, if I set the value to Top, Left, or Bottom, the tooltips appear in the correct place.

推荐答案

code> Setter 和 Getter 只是用于编码的.NET包装器。从XAML绑定时,将调用 GetXXXX SetXXX ,而不是属性 Getter Setter

The Setter and Getter are only the .NET Wrappers for your coding. When Binding from XAML the GetXXXX and SetXXX will be called and not the Property Getter and Setter.

您还可以访问 DependencyProperty PropertyChanged 回调,每次属性更改时都会调用此函数。

You can also access the DependencyProperty PropertyChanged Callback, this function will be called everytime the Property is changed.

这篇关于WPF Getter / Setter未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆