2012-05-08 25 views
0

私のバインディングに問題があり、私に困惑しています。私のBuildingプロパティを初めて設定するときはいつでも、私のTitle RasedTextオブジェクトのテキストは、私が期待しているものに設定されています。しかし、Buildingプロパティに新しい値を設定すると、Titleオブジェクトのテキストフィールドはまだ古い値のままです。理由は何ですか?Silverlightのバインドが更新されない

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

バインディングのソースとしてプロパティBuildingを設定しないでください。代わりに、ソースとして使用すると、(ここでthis)に結合し、またPathプロパティを指定するクラスFloorPlanのインスタンスを使用します。

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 = ... 
関連する問題