如何将XAML文本框绑定到自定义对象?

时间:2012-05-13 16:46:02

标签: c# wpf xaml data-binding

好吧,我认为这将是一块蛋糕,但显然它不适合(我是XAML / WPF新手,顺便说一句)

我有两个WPF窗口,比如Window1和EditCustomObjectWindow。编辑Window1提供的CustomObject实例需要后者。 (我遗漏了很多与此问题无关的代码)

Window1.xaml.cs:

// first window, holding an instance of CustomObject
public partial class Window1 : Window {
    public CustomObject CustomObject {
        get;
        set;
    }

    void buttonClicked(object sender, RoutedEventArgs e) {
        new Windows(CustomObject).ShowDialog();
    }

}

EditCustomObjectWindow.xaml.cs:

public partial class EditCustomObjectWindow : Window {
    public CustomObject CustomObject {
        get;
        set;
    }
    public EditCustomObjectWindow (CustomObject customObject) {
        this.CustomObject = customObject;
    }

}

EditCustomObjectWindow.xaml:

<TextBox
        Height="22"
        Margin="5"
        Width="150"
        Text="{Binding Path=CustomObject.SomeProperty, Mode=TwoWay, PresentationTraceSources.TraceLevel=High}"></TextBox>

当然,CustomObject有SomeProperty的公共访问器。 单击该按钮时,以下信息将跟踪到输出窗口。但不知何故,我无法将其转化为问题的根源。

System.Windows.Data Warning: 54 : Created BindingExpression (hash=31654880) for Binding (hash=40799911)
System.Windows.Data Warning: 56 :   Path: 'CustomObject.SomeProperty'    System.Windows.Data Warning: 59 : BindingExpression (hash=31654880): Default update trigger resolved to LostFocus
System.Windows.Data Warning: 60 : BindingExpression (hash=31654880): Attach to         
System.Windows.Controls.TextBox.Text (hash=58067579)
System.Windows.Data Warning: 65 : BindingExpression (hash=31654880): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=31654880): Found data context element: TextBox (hash=58067579) (OK)
System.Windows.Data Warning: 69 : BindingExpression (hash=31654880): DataContext is null
System.Windows.Data Warning: 63 : BindingExpression (hash=31654880): Resolve source deferred
System.Windows.Data Warning: 65 : BindingExpression (hash=31654880): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=31654880): Found data context element: TextBox (hash=58067579) (OK)
System.Windows.Data Warning: 69 : BindingExpression (hash=31654880): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=31654880): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=31654880): Found data context element: TextBox (hash=58067579) (OK)
System.Windows.Data Warning: 69 : BindingExpression (hash=31654880): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=31654880): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=31654880): Found data context element: TextBox (hash=58067579) (OK)
System.Windows.Data Warning: 69 : BindingExpression (hash=31654880): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=31654880): Resolving source  (last chance)
System.Windows.Data Warning: 68 : BindingExpression (hash=31654880): Found data context element: TextBox (hash=58067579) (OK)
System.Windows.Data Warning: 76 : BindingExpression (hash=31654880): Activate with root item <null>
System.Windows.Data Warning: 104 : BindingExpression (hash=31654880):   Item at level 0 is null - no accessor
System.Windows.Data Warning: 101 : BindingExpression (hash=31654880): Replace item at level 1 with {NullDataItem}
System.Windows.Data Warning: 78 : BindingExpression (hash=31654880): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 86 : BindingExpression (hash=31654880): TransferValue - using fallback/default value ''
System.Windows.Data Warning: 87 : BindingExpression (hash=31654880): TransferValue - using final value ''

2 个答案:

答案 0 :(得分:2)

在你的Binding中,你必须将RelativeSource设置为祖先EditCustomObjectWindow,如下所示:

{Binding Path=CustomObject.SomeProperty, RelativeSource={RelativeSource AncestorType={x:Type local:EditCustomObjectWindow}}, Mode=TwoWay, PresentationTraceSources.TraceLevel=High}

答案 1 :(得分:0)

问题是您传递给new EditCustomObjectWindow(CustomObject).ShowDialog);的实例 是空的。

拥有与类型名称相同的属性可能有点令人困惑,也许你会new EditCustomObjectWindow(new CustomObject()).ShowDialog);

在日志中,您可以看到:

System.Windows.Data Warning: 76 : BindingExpression (hash=31654880): Activate with root item <null> 
System.Windows.Data Warning: 104 : BindingExpression (hash=31654880):   Item at level 0 is null - no accessor 
System.Windows.Data Warning: 101 : BindingExpression (hash=31654880): Replace item at level 1 with {NullDataItem} 

告诉我们找到了“第一个”属性(CustomObject),但是它为null,因此该值被NullDataItem

替换