I have a TextBox defined as below. It is done this way to deal with the screwed up editing that occurs in .NET 4+ when UpdateSourceTrigger is PropertyChanged (needed for character by character validation).
The problem. When I enter text in the TextBox it is fine. As soon as focus is lost the TextBox is blank. When I move the cursor back in - the previously entered number is back again. If I set breakpoints in code then Item.amount and drvi.item("amount") are both correct.
<TextBox x:Name="txtAmount" Margin="170,35,0,0" Width="150" Validation.ErrorTemplate="{StaticResource ValidationTemplate}">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource RoundedTextBox}">
<Setter Property="Text">
<Setter.Value>
<Binding Path="Item.amount" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type l:ItemView}}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" StringFormat="#,##0.00">
<Binding.ValidationRules>
<l:CurrencyRule />
</Binding.ValidationRules>
</Binding>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Text">
<Setter.Value>
<Binding Path="Item.amount" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type l:ItemView}}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<l:CurrencyRule />
</Binding.ValidationRules>
</Binding>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
It is bound to the following in the underlying class
Private _Item As DataRowView
Public Property Item As DataRowView
Get
Return _Item
End Get
Set(value As DataRowView)
_Item = value
NotifyPropertyChanged("Item")
End Set
End Property
Which is set to equal a DataRowView:
strSelect = "SELECT * FROM Items WHERE ID=0"
dsicmd = New OleDbDataAdapter(strSelect, cn)
dsi.Clear()
dsicmd.Fill(dsi, "Item")
dvi = New DataView(dsi.Tables.Item("Item"))
drvi = dvi.AddNew
Item = drvi
All help appreciated. Thanks Andy