WPF DataGrid验证/绑定模式错误 [英] WPF DataGrid validation/binding mode bug

查看:222
本文介绍了WPF DataGrid验证/绑定模式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个非常简单的新项目,仅测试Microsoft WPF DataGrid行为。没有其他涉及,我只使用标准的DataGrid:

I created a new project, very simple, to test only the Microsoft WPF DataGrid behavior. Nothing else involved, I only use the standard DataGrid:

<Window x:Class="MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

    <DataGrid ItemsSource="{Binding Employees, Mode=TwoWay}"
              x:Name="tlv"
              AutoGenerateColumns="False"
              SelectionMode="Extended"
              CanUserAddRows="true"
              SelectionUnit="CellOrRowHeader">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First Name" Binding="{Binding FirstName, Mode=TwoWay}"/>
            <DataGridTextColumn Header="Last Name" Binding="{Binding LastName, Mode=TwoWay}"/>
            <DataGridTextColumn Header="Salary" Binding="{Binding Salary, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=LostFocus}"/>
        </DataGrid.Columns>
    </DataGrid>
</Window>

代码背后:

Imports System.Collections.ObjectModel
Imports DataGridTest.Data

Class MainWindow
Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Me.DataContext = Me
    Employees = New ObservableCollection(Of Employee)(EmployeeRepository.GetFlatListData())
    BindableSelectedItems = New ObservableCollection(Of Object)
End Sub


Private _employees As ObservableCollection(Of Employee)

Public Property Employees() As ObservableCollection(Of Employee)
    Get
        Return _employees
    End Get
    Set(ByVal value As ObservableCollection(Of Employee))
        _employees = value
    End Set
End Property


Private _bindableSelectedItems As ObservableCollection(Of Object)
Public Property BindableSelectedItems() As ObservableCollection(Of Object)
    Get
        Return _bindableSelectedItems
    End Get
    Set(value As ObservableCollection(Of Object))
        'Set the new value of BindableSelectedItems
        _bindableSelectedItems = value
    End Set
End Property


Private _selectedEmployeeForSelectedItemsSimulation As Employee
Public Property SelectedEmployeeForSelectedItemsSimulation() As Employee
    Get
        Return _selectedEmployeeForSelectedItemsSimulation
    End Get
    Set(value As Employee)
        'Set the new value of SelectedEmployeeForSelectedItemsSimulation
        _selectedEmployeeForSelectedItemsSimulation = value

        If _selectedEmployeeForSelectedItemsSimulation IsNot Nothing AndAlso BindableSelectedItems IsNot Nothing Then
            BindableSelectedItems.Clear()
            BindableSelectedItems.Add(value)
        End If
    End Set
End Property
End Class

员工类实现IDataErrorInfo:

The Employee class, which implements IDataErrorInfo:

Imports Microsoft.Practices.Prism.ViewModel
Imports System.Collections.ObjectModel
Imports System.ComponentModel

Namespace Data
Public Class Employee
    Inherits NotificationObject
    Implements IDataErrorInfo

    Public Sub New()

    End Sub

    Public Sub New(ByVal fName As String, ByVal lName As String, ByVal salary As Double)
        FirstName = fName
        LastName = lName
        Me.Salary = salary
    End Sub


    Private _firstName As String
    Public Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set(value As String)
            'Set the new value of FirstName
            _firstName = value

            'Warn any Observers that the FirstName have changed.
            RaisePropertyChanged(Function() Me.FirstName)
        End Set
    End Property


    Private _lastName As String
    Public Property LastName() As String
        Get
            Return _lastName
        End Get
        Set(value As String)
            'Set the new value of LastName
            _lastName = value

            'Warn any Observers that the LastName have changed.
            RaisePropertyChanged(Function() Me.LastName)
        End Set
    End Property


    Private _isSelected As Boolean
    Public Property IsSelected() As Boolean
        Get
            Return _isSelected
        End Get
        Set(value As Boolean)
            'Set the new value of IsSelected
            _isSelected = value

            'Warn any Observers that the IsSelected have changed.
            RaisePropertyChanged(Function() Me.IsSelected)
        End Set
    End Property

    Private _salary As Double
    Public Property Salary() As Double
        Get
            Return _salary
        End Get
        Set(value As Double)
            'Set the new value of Salary
            _salary = value

            'Warn any Observers that the Salary have changed.
            RaisePropertyChanged(Function() Me.Salary)
        End Set
    End Property



    Public ReadOnly Property [Error] As String Implements IDataErrorInfo.Error
        Get
            Return String.Empty
        End Get
    End Property

    Default Public ReadOnly Property Item(columnName As String) As String Implements IDataErrorInfo.Item
        Get

            If Me.Salary <= 0 Then
                Return "The salary must be positive."
            End If

            Return String.Empty
        End Get
    End Property
End Class
End Namespace

到目前为止,这么好。然后,在添加新行后,尝试更新工资值时,会发生什么:

So far, so good. Then, here is what happens when I try to update the salary value after having added a new row:

该值重置为0!


  1. 为什么这样做?使用DataGrids的方式有问题吗?

  2. 是否有解决方法?

...和解决方案

我终于找到了一个什么样的解决方案,即使我还不知道什么原因我现在认为是Microsoft DataGrid中的一个错误。

I finally found what looks like a solution, even if I still don't know the reason of what I now consider as a bug in the Microsoft DataGrid.

如果在具有验证的datagrid列中指定了绑定模式,则; if 而不是我不指定绑定模式,一切都很好

If a binding mode is specified in the datagrid column with a Validation, the bug occurs; if instead I do not specify the binding mode, everything is fine.

<DataGridTextColumn Header="Salary" Binding="{Binding Salary, ValidatesOnDataErrors=True, UpdateSourceTrigger=LostFocus}"/>

我不太明白,只要我知道默认绑定模式是TwoWay ...嗯,至少我解决了我的问题。

What I do not quite understand though, is that as far as I know the default binding mode is TwoWay... Well, at least I solved my problem.

我在Microsoft Connect上打开了一个错误报告, here

I opened a bug report on Microsoft Connect, here

推荐答案

只是为了让你知道,我从 微软 收到消息,他们说这并不重要,甚至尝试解决这个错误。

just to let you know, I got news from Microsoft, they say this is not important and they won't even try to solve the bug.

看看 here

所以,这是一个bug,一会儿d不会很快修复。

So, this is a bug that has been around for a while, and won't be fixed any time soon.

这篇关于WPF DataGrid验证/绑定模式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆