银光绑定未更新

本文关键字:更新 绑定 | 更新日期: 2023-09-27 18:33:36

我的绑定问题让我难倒了。每当我第一次设置 Building 属性时,我的 Title RasedText对象的文本都会设置为我期望的文本。但是,当我为 Building 属性设置新值时,Title 对象的文本字段仍保留旧值。有什么想法吗?

public static readonly DependencyProperty buildingProperty = DependencyProperty.Register
(
    "building",
    typeof(string),
    typeof(FloorPlan),
    new PropertyMetadata((d,e) => 
        {
            try
            {
                (d as FloorPlan).BuildingChanged();
            } catch {}
        }
));
public string Building
{
    get { return (string)GetValue(buildingProperty); }
    set { SetValue(buildingProperty, value); }
}
private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
{
    //Code...
    Binding binding = new Binding();
    binding.Source = Building;
    binding.Mode = BindingMode.OneWay;
    Title.SetBinding(TextControls.RaisedText.TextProperty, binding);
    //Code...
}

银光绑定未更新

不应将属性Building设置为绑定的源。相反,您可以将要绑定到的类FloorPlan的实例用作源(此处this),并指定 Path 属性:

Binding binding = new Binding(); 
binding.Source = this;
binding.Path = new PropertyPath("Building"); 
// no need for the following, since it is the default
// binding.Mode = BindingMode.OneWay; 
Title.SetBinding(TextControls.RaisedText.TextProperty, binding);

仅当您遵守属性命名约定并使用以大写字符开头的适当名称声明Building时,这也将起作用:

public static readonly DependencyProperty buildingProperty =
    DependencyProperty.Register("Building", typeof(string), typeof(FloorPlan), ...); 

像这样声明它也是标准的,因为它是一个公共类成员:

public static readonly DependencyProperty BuildingProperty = ...