DependencyProperty 依赖项和 PropertyCallbacks

发布于 2024-09-13 19:19:12 字数 1538 浏览 1 评论 0原文

这篇文章的核心问题是,您是否可以期望在设置其中一个 DP 的属性回调时设置所有 DP。我问这个是因为这不是我看到的行为。

一个类有两个 DP,这两个 DP 都是在 XAML 中设置的,如下所示:

<!-- Days of the Week -->
<local:DayOfTheWeekColumn 
    DowIndex="0" 
    ActivityCollection="{Binding Source={StaticResource spy}, Path=DataContext}"
    ....                    
/>

在 DayOfTheWeekColumn 类中,暂时如下声明 DP:

public static readonly DependencyProperty DowIndexProperty = DependencyProperty.RegisterAttached(
        "DowIndex", typeof(string), typeof(DayOfTheWeekColumn), 
        new PropertyMetadata(OnDowIndexSet), IsIndexValid);

    public static readonly DependencyProperty ActivityCollectionProperty = DependencyProperty.RegisterAttached(
        "ActivityCollection", typeof(IActivityCollectionViewModelBase), typeof(DayOfTheWeekColumn), 
        new PropertyMetadata(OnActivityCollectionSet));

当执行 OnDowIndexSet 回调时,ActivityCollectionProperty 仍为 null,但当执行 OnActivityCollectionSet 回调时,DowIndexProperty 被估值。我需要这两个属性来完成这个用例。这是 OnActivityCollectionSet:

    private static void OnActivityCollectionSet(DependencyObject target, DependencyPropertyChangedEventArgs e) {
        var context = (IActivityCollectionViewModelBase) e.NewValue;
        var col = (DayOfTheWeekColumn) target;
        var index = Convert.ToInt32(col.DowIndex);
        _setHeader(col, context, index);
    }

现在这可以工作,但只要我不了解在执行回调时设置这两个属性的时间,它对我来说就很脆弱。为什么这两个属性都可用于 OnActivityCollectionSet 而不是 OnDowIndexSet?

干杯,
贝里尔

The heart of the question in this post is whether you can expect all DPs to be set by the time a Property callback for one of them is set. I ask this because this is not the behavior I am seeing.

A class has two DP's, both of which are set in XAML, like so:

<!-- Days of the Week -->
<local:DayOfTheWeekColumn 
    DowIndex="0" 
    ActivityCollection="{Binding Source={StaticResource spy}, Path=DataContext}"
    ....                    
/>

In the DayOfTheWeekColumn class, the DPs are declared like so, for now:

public static readonly DependencyProperty DowIndexProperty = DependencyProperty.RegisterAttached(
        "DowIndex", typeof(string), typeof(DayOfTheWeekColumn), 
        new PropertyMetadata(OnDowIndexSet), IsIndexValid);

    public static readonly DependencyProperty ActivityCollectionProperty = DependencyProperty.RegisterAttached(
        "ActivityCollection", typeof(IActivityCollectionViewModelBase), typeof(DayOfTheWeekColumn), 
        new PropertyMetadata(OnActivityCollectionSet));

When the OnDowIndexSet callback executes, the ActivityCollectionProperty is still null, but when the OnActivityCollectionSet callback executes, the DowIndexProperty is valued. I need both properties to accomplish this use case. Here is OnActivityCollectionSet:

    private static void OnActivityCollectionSet(DependencyObject target, DependencyPropertyChangedEventArgs e) {
        var context = (IActivityCollectionViewModelBase) e.NewValue;
        var col = (DayOfTheWeekColumn) target;
        var index = Convert.ToInt32(col.DowIndex);
        _setHeader(col, context, index);
    }

Now this works, but it is fragile to me as long as I don't understand the timing of setting both properties by the time the callbacks execute. Why should both properties be available for OnActivityCollectionSet and not OnDowIndexSet?

Cheers,
Berryl

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

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

发布评论

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

评论(1

戏蝶舞 2024-09-20 19:19:12

也许在 XAML 中的 ActivityCollection 之前设置 DowIndex 之前?

您不能使用默认的 DependencyProperty 值来防止此问题吗?

小提示:Path 绑定属性是默认属性,因此您可以使用以下较短的表示法:

ActivityCollection="{Binding DataContext, Source={StaticResource spy}}"

Maybe before the DowIndex is set before ActivityCollection in XAML?

And couldn't you use default DependencyProperty value to prevent this issue?

And small OT tip: The Path binding property is a default one so you could use this shorter notation:

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