2013-10-29 69 views
1

ValidatesOnDataErrors=True當我的DataGridTextColumn內容無效值時,如果我編程改變這個值,它會顯示改變後的值,但是直到你點擊進入列(進入編輯模式),該值將恢復到一個無效... 下面是一個工作示例: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=TrueDataGridTextColumn.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> 

回答

2

嘗試添加Mode=TwoWayBinding代碼,它爲我工作!

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

謝謝!它也適用於我,我認爲'Mode = TowWay'是默認值,但它確實有效。 – Bolu