2

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

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

复制:

例如:输入"ADS"number 字段无效的Column,然后将其更改为"1"使用a button,它将显示1 在列中,但是一旦单击单元格并进入编辑模式,值将ADS再次变回.

当前解决方法:

为了清楚起见,我目前的解决方法是ValidatesOnDataErrors=True删除DataGridTextColumn.EditingElementStyle

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

1 回答 1

2

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

Binding="{Binding  Price, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True,Mode=TwoWay}"
于 2013-10-31T09:31:11.797 回答