c# - DataGridTextColumn 值不断变回(如果无效)

标签 c# wpf validation wpfdatagrid

使用 ValidatesOnDataErrors=True 当我 DataGridTextColumn 内容无效值时,如果我以编程方式更改该值,它将显示更改后的值,但直到您单击该列(进入编辑模式),该值将恢复为无效值... 以下是一个工作示例:

XAML:

<Window x:Class="WpfApplication1.Window13"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window13" Height="300" Width="300">
<Grid>
    <DataGrid VerticalAlignment="Top" Margin="0,5"  CanUserAddRows="True"  AutoGenerateColumns="False"  VerticalScrollBarVisibility="Auto"  ItemsSource="{Binding Pricelist}" CanUserDeleteRows="True" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Price" Width="60" Binding="{Binding Price, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>                
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="Correct" Height="23" HorizontalAlignment="Left" Margin="191,226,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>

代码隐藏:

using System;
using System.Windows;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfApplication1
{
    public partial class Window13 : Window
    {
        public Window13()
        {
            InitializeComponent();
            Pricelist = new ObservableCollection<MyProduct>();
            this.DataContext = this;
        }

        public ObservableCollection<MyProduct> Pricelist { get; set; }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            foreach(var p in Pricelist)
            {
                p.Price = "0";
            }
        }
    }

    public class MyProduct:INotifyPropertyChanged,IDataErrorInfo
    {
        private string _price;
        public string Price
        {
            get
            {
                return _price;
            }
            set
            {
                _price = value;
                this.RaisePropertyChanged("Price");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        protected void RaisePropertyChanged(String propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
        public string Error
        {
            get
            {
                return this["Price"];
            }
        }
        public string this[string columnName]
        {
            get
            {
                string result = string.Empty;
                switch (columnName)
                {
                    case "Price":
                        {
                            decimal temdecimal = 0.00m;

                            if (string.IsNullOrEmpty(Price) || string.IsNullOrWhiteSpace(Price)
                                || !decimal.TryParse(Price, out temdecimal))
                            {
                                result = "Price is invalid";
                            }
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                return result;
            }
        }
    }
}

重现:

E.g.: input "ADS" into the Column which is invalid for a number field, and then change it to "1" use a button, it will display 1 in the column, but as soon as you click the cell and enter into the edit mode, the value will change back to ADS again.

当前解决方法:

为了明确起见,我当前的解决方法是删除 DataGridTextColumn.EditingElementStyle 中的 ValidatesOnDataErrors=True:

<DataGridTextColumn Header="Price" Width="60">
     <DataGridTextColumn.ElementStyle>
          <Style TargetType="TextBlock">
                 <Setter Property="Text" Value="{Binding Price,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>
          </Style>
     </DataGridTextColumn.ElementStyle>
     <DataGridTextColumn.EditingElementStyle>
          <Style TargetType="TextBox">
                 <Setter Property="Text" Value="{Binding Price,UpdateSourceTrigger=PropertyChanged}"/>
          </Style>
     </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

最佳答案

尝试将 Mode=TwoWay 添加到您的 Binding 代码中,它对我有用!

Binding="{Binding  Price, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True,Mode=TwoWay}"

关于c# - DataGridTextColumn 值不断变回(如果无效),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19663281/

相关文章:

c# - 如何在 HDF5DotNet 中创建 2D H5Array

javascript - 使用 ng-messages 对生成的字段进行表单验证

c# - 复选框内容中日期的字符串格式

c# - 基于其他值的自定义验证

php - 在php中验证日期格式

c# - Windows Server 2008 R2 上的 WPF 样式

c# - 解析字符串并在 C# 中查找特定文本

c# - 如何使用 web.config 文件强制使用 HTTPS

c# - 在行为中使用静态资源

c# - System.Drawing 是否应该仅在 WPF 的 UI 线程(C#)上运行?