Silverlight绑定不更新

时间:2012-05-08 16:25:22

标签: c# .net wpf silverlight data-binding

我的绑定问题令我难过。每当我第一次设置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...
}

1 个答案:

答案 0 :(得分: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 = ...