C# 绑定到用户控件';的DependencyProperty未调用setter

C# 绑定到用户控件';的DependencyProperty未调用setter,c#,xaml,dependency-properties,windows-phone-8.1,C#,Xaml,Dependency Properties,Windows Phone 8.1,我的页面xaml: <header:DefaultText x:Name="header" HeaderText="{Binding Resources.HeaderTitle}"/> 我的问题是,当我使用绑定设置HeaderText属性时,不会调用setter,但当我使用未绑定的普通字符串时,会调用setter 我已经尝试过类似问题的答案,如:XAML绑定内部不调用Setter方法,而是直接设置依赖项属性的值,正如所指出的: WPF XAML处理器使用属性系统方法作为依赖项 加载

我的页面xaml:

<header:DefaultText x:Name="header" HeaderText="{Binding Resources.HeaderTitle}"/>
我的问题是,当我使用绑定设置HeaderText属性时,不会调用setter,但当我使用未绑定的普通字符串时,会调用setter


我已经尝试过类似问题的答案,如:

XAML绑定内部不调用Setter方法,而是直接设置依赖项属性的值,正如所指出的:

WPF XAML处理器使用属性系统方法作为依赖项 加载二进制XAML和处理以下属性时的属性 依赖属性。这实际上绕过了属性 包装纸。实现自定义依赖项属性时,必须 请解释此行为,并应避免在中放置任何其他代码 除属性系统方法GetValue之外的属性包装器 和设置值

您需要做的是注册一个回调方法,该方法在依赖项属性更改时激发:

public static DependencyProperty HeaderTextProperty = DependencyProperty.Register(
    "HeaderText", 
    typeof(string), 
    typeof(DefaultText), 
    new PropertyMetadata(string.Empty, PropertyChangedCallback)
);

private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
    // this is the method that is called whenever the dependency property's value has changed
}

感谢您的快速回复和解释,它似乎仍然不起作用,我可能会错过其他东西吗?PropertyChangedCallback在我不使用绑定但在使用绑定时不会被调用。可能您的依赖项属性根本没有更新,请尝试向绑定添加
{binding…,Mode=TwoWay}
以强制双向更新?我将其更改为
HeaderText=“{binding Resources.HeaderTitle,Mode=TwoWay}”
但它似乎仍然不起作用。当我在正常控件(如TextBlock.Text)而不是用户控件上使用它时,绑定确实可以工作。另一个想法是:您可以检查
资源.headertile
(或您绑定到的任何对象)在评估绑定时是否真的包含一些数据吗?如果绑定值等于dependency属性的默认值(在您的示例中为
string.Empty
),则不会触发回调。这就是问题所在,转换器似乎错过了该值。谢谢!可能重复的
public static DependencyProperty HeaderTextProperty = DependencyProperty.Register(
    "HeaderText", 
    typeof(string), 
    typeof(DefaultText), 
    new PropertyMetadata(string.Empty, PropertyChangedCallback)
);

private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
    // this is the method that is called whenever the dependency property's value has changed
}