0

好的,我认为这将是小菜一碟,但显然它不适合(我是 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 ''
4

2 回答 2

2

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

{Binding Path=CustomObject.SomeProperty, RelativeSource={RelativeSource AncestorType={x:Type local:EditCustomObjectWindow}}, Mode=TwoWay, PresentationTraceSources.TraceLevel=High}
于 2012-05-13T17:02:05.760 回答
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找到了“第一个”属性 ( ),但它为空,因此该值被替换为NullDataItem

于 2012-05-13T16:53:31.853 回答