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 に答える