设置依赖属性后调用的 C# 函数

发布于 2024-08-25 01:05:31 字数 816 浏览 4 评论 0原文

我的代码目前如下所示:

private Foo myFoo;

public Foo CurrentFoo
{
    get { return myFoo; }
    set { SetFoo(value); }
}

private void SetFoo(Foo newFoo)
{
    // Do stuff
    // Here be dragons

    myFoo = newFoo;
}

为了能够在 XAML/WPF 中绑定它,我需要将 Foo 转换为依赖属性:

public static DependencyProperty CurrentFooProperty = 
    DependencyProperty.Register("CurrentFoo", typeof(Foo), 
        typeof(FooHandler), new PropertyMetadata(false));        

public Foo CurrentFoo
{
    get { return (Foo)GetValue(CurrentFooProperty); }
    set { SetValue(CurrentFooProperty, value); }
}

我听说您不应该在实际的 C# 属性集 {} 中执行魔法,因为它可能不会被调用,但值会直接写入依赖属性。如果这是错误的,请告诉我,这似乎是最明显、最简单的路线。

我知道我可以向依赖属性添加验证函数,但我认为它不应该用于此目的?我需要将更改传达给尚未在 XAML 中绑定的遗留系统。

解决这个问题的最佳方法是什么?

My code currently looks like this:

private Foo myFoo;

public Foo CurrentFoo
{
    get { return myFoo; }
    set { SetFoo(value); }
}

private void SetFoo(Foo newFoo)
{
    // Do stuff
    // Here be dragons

    myFoo = newFoo;
}

To be able to bind it in XAML/WPF I need to turn Foo into a dependency property:

public static DependencyProperty CurrentFooProperty = 
    DependencyProperty.Register("CurrentFoo", typeof(Foo), 
        typeof(FooHandler), new PropertyMetadata(false));        

public Foo CurrentFoo
{
    get { return (Foo)GetValue(CurrentFooProperty); }
    set { SetValue(CurrentFooProperty, value); }
}

Ive heard that you shouldnt do magic inside the actual C# property set {}, since it might not be called but the value is written directly to the dependency property. If this is false, let me know, it seems like the most obvious and simple route to take.

I know I can add a validation function to the dependency property but I assume that it shouldnt be used for this? I need to communicate the change to legacy systems that cannot yet be bound in XAML.

Whats the best way to approach this problem?

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

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

发布评论

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

评论(2

那小子欠揍 2024-09-01 01:05:31
public static DependencyProperty CurrentFooProperty = DependencyProperty.Register(
    "CurrentFoo",
    typeof(Foo), 
    typeof(FooHandler),
    new PropertyMetadata(OnCurrentFooChanged));

private static void OnCurrentFooChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var currentFoo = (Foo) e.NewValue;

    // Use
}
public static DependencyProperty CurrentFooProperty = DependencyProperty.Register(
    "CurrentFoo",
    typeof(Foo), 
    typeof(FooHandler),
    new PropertyMetadata(OnCurrentFooChanged));

private static void OnCurrentFooChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var currentFoo = (Foo) e.NewValue;

    // Use
}
神仙妹妹 2024-09-01 01:05:31

这应该由分配给依赖属性的回调来处理。正确的位置取决于这里实际发生的情况:

// Do stuff
// Here be dragons

可以分配给依赖属性的三个回调 - 如果您只需要通知旧系统,那么我建议添加一个 属性更改回调,并在其中添加通知。

但是,如果这是通过旧系统执行相当于验证的操作,则将这些龙放入 ValidationCallback。

鉴于您的情况,我怀疑 CoerceValue 回调是否合适。

This should be handled by a callback assigned to the Dependency Property. The proper place for this depends on what is actually happening here:

// Do stuff
// Here be dragons

There are three callbacks that can be assigned to a Dependency Property - If you just need to notify the legacy systems, then I recommend adding a Property Changed callback, and adding the notification there.

If, however, this is performing something that amounts to validation via the Legacy system, it may be more appropriate to put these dragons into the ValidationCallback.

I doubt that the CoerceValue callback is appropriate, given your scenario.

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