未调用 DependencyProperty 回调 [英] DependencyProperty callback not called

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

问题描述

我目前正在使用 C#/WPF 做一个用户控件,并且我正在使用一些 DependencyProperty 对象.

I'm currently doing an usercontrol with C#/WPF and i'm using some DependencyProperty objects.

我想做的是当值发生变化时,我们调用一个回调方法来处理一些数据......我看到有一个PropertyChangedCallback类用于这个目的,但它不起作用..

What I want to do is when the value changes, we call a callback method to process some data... I saw that there is a PropertyChangedCallback class for this purpose, but it doesn't work..

这是我的代码:

用户控件:

public partial class TimeLine : UserControl
{
    public static readonly DependencyProperty FramecountProperty = DependencyProperty.Register("FrameCount", typeof(Int32), typeof(TimeLine), new FrameworkPropertyMetadata(0, new PropertyChangedCallback(FrameCountChanged)));

    public Int32 FrameCount
    {
        get { return (Int32)this.GetValue(FramecountProperty); }
        set { this.SetValue(FramecountProperty, value); }
    }

    // More code...

    public static void FrameCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Do stuff
    }
}

xml:

<!-- Time line container -->
<controls:TimeLine Grid.Row="2" Header="Storyboard" FrameCount="{Binding FrameCount}" />

视图模型:

private Int32 frameCount;
public Int32 FrameCount
{
    get { return this.frameCount; }
    // this is from: https://github.com/ShyroFR/CSharp-Elegant-MVVM
    set { this.NotifyPropertyChanged(ref this.frameCount, value); }
}

public MainViewModel()
{
    this.FrameCount = 42;
}

我做错了吗?

感谢您的帮助.

推荐答案

我找到了解决方案,通过找到祖先.

I've found a solution, by finding the ancestor.

<controls:TimeLine Grid.Row="2" Header="Storyboard" FrameCount="{Binding Path=DataContext.FrameCount, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />

感谢您的帮助!

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

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