与datagrid绑定时的验证文本框

时间:2016-02-02 14:36:01

标签: vb.net validation mvvm idataerrorinfo

我有这个Grid与" SelectedItem绑定SelectRowGrid"

     <DataGrid 
            ItemsSource="{Binding Dati_Cliente, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
            SelectedItem="{Binding SelectRowGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  
            Style="{DynamicResource ST_DataGrid}" 
            CellStyle="{DynamicResource St_DataGridCellStyle}" SelectionMode="Single"  Name="Dg_Dati">

                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Path=Id_Cli}" Header="Id Cliente"  Width="Auto" />
                    <DataGridTextColumn Binding="{Binding Path=Desc}" Header="Descrizione" Width="Auto" />

等...

Id_Cli 描述位于绑定2个文本框

                <TextBox Grid.Column="1"  Grid.Row="0" Text="{Binding SelectRowGrid.Id_Cli, 
                UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, 
                ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Margin="4,4,4,4"  MaxLength="11"/>

            <TextBox Grid.Column="1"  Grid.Row="1" Text="{Binding SelectRowGrid.Desc, 
                UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, 
                ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Margin="4,4,4,4"   MaxLength="100"/>

每次单击一行时,文本框都会毫无问题地填充,如果我更改文本值,则此新值将在行网格上更新。

但这是我的问题。为什么,如果thextbox为空,我看不到文本框周围的红色边框?

为什么这段代码不起作用。

        Public ReadOnly Property [Error]() As String Implements System.ComponentModel.IDataErrorInfo.Error
        Get
            Return Nothing
        End Get
    End Property
    Default Public ReadOnly Property Item(ByVal Name As String) As String Implements System.ComponentModel.IDataErrorInfo.Item
        Get
            Dim result As String = Nothing
            If Name = "SelectRowGrid" Then

                If SelectRowGrid IsNot Nothing Then
                    If IsNothing(SelectRowGrid.Id_Cli) Then
                        result = "Item number cannot be blank!"
                    End If
                    If IsNothing(SelectRowGrid.Desc) Then
                        result = "Item number cannot be blank!"
                    End If
                End If
            End If
            Return result
        End Get
    End Property

P.S。在我的代码中,我实现了IDataErrorInfo和INotifyPropertyChanged,

固定。 我在MyClass类

中更改并移动了此代码

新守则:

        Public ReadOnly Property [Error]() As String Implements System.ComponentModel.IDataErrorInfo.Error
        Get
            Return Nothing
        End Get
    End Property
    Default Public ReadOnly Property Item(ByVal Name As String) As String Implements System.ComponentModel.IDataErrorInfo.Item
        Get
            Dim result As String = Nothing
            If Name.Equals("Id_Cli") Then
                If String.IsNullOrEmpty(Me.Id_Cli) Then
                    Return "Error !"
                End If
            End If
            If Name.Equals("Desc") Then
                If String.IsNullOrEmpty(Me.Desc) Then
                    Return "Error !"
                End If
            End If

            Return result
        End Get
    End Property

0 个答案:

没有答案