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