简体   繁体   中英

How to bind XAML textbox to custom object?

Okay I thought this is going to be a piece of cake, but obviously it is not for (I am a XAML/WPF newbie, btw)

I have got two WPF windows, say Window1 and EditCustomObjectWindow. The latter is needed to edit a CustomObject instance provided by Window1. (I have left out a lot of code irrelevant for this problem)

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>

Of course, CustomObject has a public accessor for SomeProperty. When clicking the button, the following information is traced to the output window. But somehow, I am not able to translate it to the source of the problem.

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 ''

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

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

The problem is that the instance you pass in to new EditCustomObjectWindow(CustomObject).ShowDialog); is null.

It can be a little confusing to have properties that have the same names as types, maybe you ment new EditCustomObjectWindow(new CustomObject()).ShowDialog); ?

In the log you can see:

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} 

telling us that the "first" property ( CustomObject ) is found, but that its null, so the value is replaced by a NullDataItem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM