2012-04-10 101 views
1

我對WPF中的datagrid有一個奇怪的問題。我爲我的應用程序使用MVVM模式,我的視圖模型實現了idataerrorinfo接口。每當我在添加新行後在數據網格中上下滾動時,所有單元格都混亂起來,整個數據網格凍結。它工作正常,如果我刪除了idataerrorinfo接口實現。任何人都有同樣的問題?Datagrid在滾動時掛起/凍結

.ANY幫助將不勝感激......

更新: 後,我添加一個新行DataGrid中的怪異的行爲只發生。如果我修改現有的行並上下滾動不會導致任何問題。將新的視圖模型添加到可觀察集合時發生某些事情。不知道是什麼。需要一些幫助..

UPDATE: 下面是該項目的一個小版本 XAML

<Window x:Class="testWPF.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 

    <!-- style to apply to DataGridTextColumn in edit mode --> 
    <Style x:Key="CellEditStyle" TargetType="{x:Type TextBox}"> 
     <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="Padding" Value="0"/> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="true"> 
       <Setter Property="ToolTip" 
         Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

    <!-- A Row Style which renders a different validation error indicator --> 
    <Style x:Key="RowStyle" TargetType="{x:Type dg:DataGridRow}"> 
     <Setter Property="ValidationErrorTemplate"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Grid> 
         <Ellipse Width="12" Height="12" Fill="Red" Stroke="Black" StrokeThickness="0.5"/> 
         <TextBlock FontWeight="Bold" Padding="4,0,0,0" Margin="0" VerticalAlignment="Top" Foreground="White" Text="!" 
            ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dg:DataGridRow}}, 
                Path=(Validation.Errors)[0].ErrorContent}"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 

<!-- a simple details view which is synchronised with the selected item in the data grid --> 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="265*" /> 
     <RowDefinition Height="46*" /> 
    </Grid.RowDefinitions> 
    <DataGrid Name="dataGrid" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True" 
       ItemsSource="{Binding GetPeople}" Height="204" Margin="0,54,0,8"> 
     <!--<dg:DataGrid.RowValidationRules> 
      <local:RowDummyValidation/> 
     </dg:DataGrid.RowValidationRules>--> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Name" EditingElementStyle="{StaticResource CellEditStyle}" 
           Binding="{Binding Path=Name, ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"/> 
      <DataGridTextColumn Header="Age" EditingElementStyle="{StaticResource CellEditStyle}" 
           Binding="{Binding Path=Age, ValidatesOnExceptions=True}"/> 
     </DataGrid.Columns> 
    </DataGrid> 
    <Button Content="Button" Command="{Binding AddNewConfigProperty}" 
      Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="194,11,0,0" 
      Name="button1" VerticalAlignment="Top" Width="75" /> 
</Grid> 

人ListViewModel

namespace testWPF 
{ 
    class PersonListViewModel: ViewModelBase 
    { 
     private ObservableCollection<Person> personCollection; 

     //private PartNumbersEntities dbCOntext = new PartNumbersEntities(); 
     public ObservableCollection<Person> GetPeople 
     { 
      get 
      { 
       if (personCollection == null) 
       { 
        personCollection = new ObservableCollection<Person>(); 
        for(int i= 0; i<100;i++) 
        { 
         personCollection.Add(new Person() 
         { 
          Name = "Frank Grimmes", 
          Age = 25, 
          DateOfBirth = new DateTime(1975, 2, 19) 
         }); 
        } 
       }     
       return personCollection; 
      }    
     } 

     public ICommand AddNewConfigProperty { get { return new RelayCommand(AddNewConfigPropertyExecute, CanAddNewConfigPropertyExecute); } } 

     bool CanAddNewConfigPropertyExecute() 
     { 
      return true; 
     } 

     void AddNewConfigPropertyExecute() 
     { 
      personCollection.Add(new Person() 
        { 
         Name = "Some Name", 
         Age = 25, 
         DateOfBirth = new DateTime(1924, 9, 1) 
        }); 
      OnPropertyChanged("GetPeople"); 
     } 
    } 
} 

Person類

namespace testWPF 
{ 
    public class Person : ViewModelBase, IDataErrorInfo 
    { 
     //private readonly Regex nameEx = new Regex(@"^[A-Za-z ]+$"); 

     private string name; 

     public string Name 
     { 
      get { return name; } 
      set 
      { 
       name = value; 
      } 
     } 

     private int age; 

     public int Age 
     { 
      get { return age; } 
      set 
      { 
       age = value; 
      } 
     } 

     public DateTime DateOfBirth { get; set; } 

     public string Error 
     { 
      get { return ""; } 
     } 

     public string this[string columnName] 
     { 
      get 
      { 
       string result = null; 
       if (columnName == "Name") 
       { 
        if (string.IsNullOrEmpty(Name)) 
         result = "Please enter a name"; 
       } 
       return result; 
      } 
     } 
    } 
} 

回答

0

不要在您的IDataErrorInfo這個[string columnName] Getter中耗時的IO操作。讓

System.IO.File.AppendAllText("C:\\temp\\log.txt", "PartConfigName: " + PartConfigName + "\r\n"); 

asynchronconditional on debug modus[Conditional("DEBUG")]

+0

我已經刪除了該IO操作部分。但是仍然有相同的錯誤。其實我只是把它放在那裏來檢查控制進行驗證的次數......現在我有了一些新的發現......請檢查更新部分。 – IamaC 2012-04-10 15:43:32

+0

在'AddNewConfigPropertyExecute()'你有兩個'AllConfig.Add()',也許就是這個問題。 PS:爲了快速檢查,我使用了'Console.Write()'。 – LPL 2012-04-10 16:49:09

+0

對不起,額外的AllConfig.Add()。我擺脫了它。但沒有解決我的問題。我創建了一個排除所有實體框架的微型項目。但仍然有同樣的問題。它只發生在我添加一行並嘗試編輯和滾動時。集合發生了一些事情。不知道是什麼。如果你喜歡我可以給你發送代碼。 – IamaC 2012-04-10 17:55:30