更新属性时如何调用代码隐藏中的方法?

发布于 2024-10-20 12:41:38 字数 1501 浏览 0 评论 0原文

我需要的是当我的视图模型上的属性更新时能够在我的视图类的代码隐藏中执行代码。我的理解是我需要使用依赖属性。

我的视图模型确实实现了 INotifyPropertyChanged。

这是我的视图模型中的属性:

private DisplayPosition statusPosition;
public DisplayPosition StatusPosition
{
    get { return this.statusPosition; }
    set
    {
        this.statusPosition = value;
        this.OnPropertyChanged("StatusPosition");
    }
}

这是我的视图中的依赖属性:

public DisplayPosition StatusPosition
{
    get { return (DisplayPosition)GetValue(StatusPositionProperty); }
    set { SetValue(StatusPositionProperty, value); }
}
public static readonly DependencyProperty StatusPositionProperty =
        DependencyProperty.Register(
        "StatusPosition",
        typeof(DisplayPosition),
        typeof(TranscriptView),
        new PropertyMetadata(DisplayPosition.BottomLeft));

这是我在视图类中设置绑定的位置(this.DataContextChanged 的处理程序):

private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    Binding myBinding = new Binding("StatusPosition");
    myBinding.Source = this.DataContext;
    myBinding.NotifyOnTargetUpdated = true;
    this.SetBinding(TranscriptView.StatusPositionProperty, myBinding);
}

当我放置在我看来,属性设置器上的断点,即使在我观察视图模型中的值更改并引发 PropertyChanged 事件后,它也永远不会被击中。最终,我的目标是能够在 setter 中放入更多代码。

如果您好奇的话,最棘手的细节是我需要根据此值在多个 StackPanel 之间移动 TextBlock。我似乎找不到仅 XAML 的方法来做到这一点。

通常,这些问题都是我忽略的简单、小、明显的事情。不过,我所做的一切都无法帮助我解决这个问题。

What I need is to be able to execute code in a code-behind for my view class when a property on my view-model is updated. My understanding is that I need to use a dependency-property.

My view-model does implement INotifyPropertyChanged.

Here is the property in my view-model:

private DisplayPosition statusPosition;
public DisplayPosition StatusPosition
{
    get { return this.statusPosition; }
    set
    {
        this.statusPosition = value;
        this.OnPropertyChanged("StatusPosition");
    }
}

Here is my dependency property in my view:

public DisplayPosition StatusPosition
{
    get { return (DisplayPosition)GetValue(StatusPositionProperty); }
    set { SetValue(StatusPositionProperty, value); }
}
public static readonly DependencyProperty StatusPositionProperty =
        DependencyProperty.Register(
        "StatusPosition",
        typeof(DisplayPosition),
        typeof(TranscriptView),
        new PropertyMetadata(DisplayPosition.BottomLeft));

Here is where I set up my binding in my view class (handler for this.DataContextChanged):

private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    Binding myBinding = new Binding("StatusPosition");
    myBinding.Source = this.DataContext;
    myBinding.NotifyOnTargetUpdated = true;
    this.SetBinding(TranscriptView.StatusPositionProperty, myBinding);
}

When I put a break-point on the setter for the property in my view, it never gets hit even after I watch the value change in the view-model, and the PropertyChanged event raised. Ultimately, my goal is to be able to put more code in the setter.

The hairy detail, if you're curious, is that I need to move a TextBlock around between multiple StackPanels based on this value. I can't seem to find a XAML-only way of doing that.

More often than not, these problems are simple little obvious things that I've missed. Nothing I'm trying is helping me sort this one out, though.

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

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

发布评论

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

评论(1

怂人 2024-10-27 12:41:38

当我在视图中的属性设置器上设置断点时,即使我观察视图模型中的值更改并引发 PropertyChanged 事件,它也永远不会被命中。最终,我的目标是能够在 setter 中放入更多代码。

你不能这样做。当您使用 DependencyProperties 时,当绑定属性更改时,永远不会调用 setter。它的唯一目的是允许您通过代码设置 DP。

相反,您需要向DP 上的元数据,并在其中添加额外的代码。当 DP 值更新时,无论是通过绑定、代码等,都会调用此函数。

When I put a break-point on the setter for the property in my view, it never gets hit even after I watch the value change in the view-model, and the PropertyChanged event raised. Ultimately, my goal is to be able to put more code in the setter.

You can't do this. When you're using DependencyProperties, the setter is never called when the bound property changes. It's only purpose is to allow you to set the DP from code.

You need to, instead, add a PropertyChangedCallback to the metadata on your DP, and add the extra code there. This will get called when the DP value updates, whether via binding, code, etc.

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