WPF依赖项控制更新 [英] WPF Dependency Control Updates

查看:49
本文介绍了WPF依赖项控制更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 TitleBar 的用户控件,其中包含以下 DependencyProperty :

I have a user control called TitleBar which contains this DependencyProperty:

public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(TitleBar), new PropertyMetadata(null));
    public string Title
    {
        get
        {
            return (string)GetValue(TitleProperty);
        }
        set
        {
            SetValue(TitleProperty, value);
        }
    }

用于用户控件的xaml如下:

The xaml for the user control looks like:

<Label Content="{Binding Title, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 

我的View xaml如下:

And my xaml of the View looks like:

<Components:TitleBar x:Name="customTitleBar" Title="Test" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3"/>

当我在保存有TitleBar用户控件的视图上设置Title ="Test"时,更改不会显示在设计器中,也不会在运行时显示.我该如何解决?

When I set Title="Test" on the View that holds the TitleBar usercontrol, the changes do not show up in the designer, nor at runtime. How can I fix this?

推荐答案

我没有完整的代码,但我的猜测是问题出在 Label 的绑定上下文中,默认情况下将是 DataContext 而不是 UserControl .

I don't have full code but my guess is that the problem is in binding context for Label, which by default will be DataContext and not UserControl.

<Label Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=Title}"

将绑定源更改为您的 UserControl 应该会有所帮助,并且您不必处理回调中的属性更新

Changing binding source to be your UserControl should help and you shouldn't have to handle property updates from callback

这篇关于WPF依赖项控制更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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