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

时间:2013-10-29 15:57:17

标签: 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;
            }
        }
    }
}

重现:

  

例如:输入"ADS"到列中,number无效   字段,然后将其更改为"1"使用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>

1 个答案:

答案 0 :(得分:2)

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

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