0

我的装订有问题,让我很困惑。每当我第一次设置我的 Building 属性时,我的 TitleRasedText对象的文本都会设置为我所期望的。但是,当我为 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...
}
4

1 回答 1

1

您不应将该属性设置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 = ...
于 2012-05-08T18:42:28.183 回答