简体   繁体   English

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

[英]DataGridTextColumn value keeps changing back (if invalid)

With ValidatesOnDataErrors=True when I DataGridTextColumn contents invalid value, if I programmingly change the value, it will display the changed value, but until you click into the Column (enter into the edit mode), the value will revert back to the invalid one... Below is a working sample: 当我DataGridTextColumn内容无效值带有ValidatesOnDataErrors=True时,如果我以编程方式更改了该值,它将显示更改后的值,但是直到您单击Column(进入编辑模式),该值才会恢复为无效值。 ..下面是一个工作示例:

XAML: 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>

CodeBehind: 代码隐藏:

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

Reproduce: 复制:

Eg: 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. 例如:在对number字段无效的列中输入"ADS" ,然后使用button将其更改为"1" ,它将在列中显示1 ,但是只要单击该单元格并进入编辑模式下,该值将再次变回ADS

Current Workaround: 当前解决方法:

Just to make it clear, the current workaround I have is to remove ValidatesOnDataErrors=True in the DataGridTextColumn.EditingElementStyle : 为了明确起见,我当前的解决方法是删除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代码中,它对我Mode=TwoWay

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM