0
votes

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

1
Does the first binding have to be two-way? I would think that is only needed when editing, so when you have focus. - Nzc
Hi. I suspect it probably doesn't. I will try and see if that makes any difference. Thanks. - Andy Powell
Unfortunately it doesn't work. Thanks. - Andy Powell

1 Answers

0
votes

Looks like the answer was to do with DataView.AddNew. If I create the new row as below it works fine. No idea why really. Debugging the bindings it looks like .AddNew does not set default field values properly.

strSelect = "SELECT * FROM Items WHERE ID=0"
dsicmd = New OleDbDataAdapter(strSelect, cn)
dsi.Clear()
dsicmd.Fill(dsi, "Item")
dsi.Tables("Items").Rows.Add()
dvi = New DataView(dsi.Tables.Item("Item"))
drvi = dvi(0)