文本框文本绑定在样式触发器中断开

时间:2015-01-30 01:00:19

标签: .net wpf binding triggers textbox

我有一个TextBox定义如下。当UpdateSourceTrigger是PropertyChanged(逐个字符验证时需要)时,这样做是为了处理.NET 4+中出现的搞乱编辑。

问题所在。当我在TextBox中输入文本时,它很好。一旦焦点丢失,TextBox就会空白。当我将光标移回时 - 之前输入的数字又回来了。如果我在代码中设置断点,那么Item.amount和drvi.item(" amount")都是正确的。

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

它与基础类

中的以下内容绑定
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

设置为等于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

所有帮助表示赞赏。 谢谢 安迪

1 个答案:

答案 0 :(得分:0)

看起来答案与DataView.AddNew有关。如果我创建如下的新行,它可以正常工作。不明白为什么真的。调试绑定它看起来像.AddNew没有正确设置默认字段值。

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)