New for WPF data binding

I am new to WPF data binding and am a bit stuck.
Apparently, my text box is incorrectly associated with the data item that I intend, and I cannot understand why.

First in my MainWindow.xamlfile I declare an object:

<Window.Resources>
    <local:Aircraft x:Key="Aircraft"/>
</Window.Resources>

This creates an object of type Aircraft during MainWindow.InitializeComponent()
(I can check this by setting a breakpoint on the airplane constructor)

A class is Aircraftdefined in a file .cswith a property Pilotthat has a property Weight, so it myAircraft.Pilot.Weightis int.

Then I try to bind a text field to this property:

<TextBox Name="PICWeight" DataContext="{Binding Source={StaticResource Aircraft}, Path=Pilot.Weight}" />

The application compiles and runs, but when I put the numeric text in a text box, then move the focus to another text box, I expect you to call the call setterfor Pilot.Weight(I have a breakpoint on it). This is not true.

I believe that there should be a standard default ValueConverter from String (from a text field) to int (type of the Weight property) and that the text fields should have an update-source event by default LostFocus.

Am I determining the binding correctly?
Do I need to create a ValueConverter or explicitly specify an update event?
Is there anything else I'm doing wrong?

+2
source share
1 answer

Text, DataContext, :

<TextBox Name="PICWeight" DataContext="{Binding Source={StaticResource Aircraft}}" Text="{Binding Path=Pilot.Weight}" />

<TextBox Name="PICWeight" Text="{Binding Source={StaticResource Aircraft}, Path=Pilot.Weight}" />
+10

All Articles