2012-04-10 3 views
1

WPF에서 DataGrid에 이상한 문제가 있습니다. 내 응용 프로그램에 대한 MVVM 패턴을 사용하고 내보기 모델은 idataerrorinfo 인터페이스를 구현합니다. 위아래로 스크롤 할 때마다 내 DataGrid에서 새 행을 추가 한 후 모든 셀이 뒤죽박죽이되어 전체 데이터 그리드가 멈 춥니 다. 그것은 idataerrorinfo 인터페이스 구현을 제거하면 잘 작동합니다. 누구나 같은 문제가 있습니까?스크롤 할 때 Datagrid 정지/고정

.ANY 도움이 ... 이해할 수있을 것이다

업데이트 : DataGrid에 새 행을 추가 한 후 이상한 동작이 발생합니다. 기존 행을 수정하고 위아래로 스크롤해도 아무런 문제가 발생하지 않습니다. 관찰 가능한 컬렉션에 새로운 viewmodel을 추가하는 동안 어떤 일이 발생합니다. 무엇을 모르겠다. 도움이 필요 ...

UPDATE : 여기 이 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 클래스

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> 

사람이 프로젝트의 작은 버전입니다

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 this [string columnName] Getter에서 시간이 많이 걸리는 IO 작업을 수행하지 마십시오. 확인

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

asynchron 또는 conditional on debug modus[Conditional("DEBUG")].

+0

IO 작업 부분을 제거했습니다. 그러나 여전히 같은 오류가 있습니다. 실제로 저는 컨트롤이 유효성 확인에 몇 번이나 도달했는지 확인하기 위해 그 안에 넣었습니다 ... 이제 새로운 발견이 있습니다 .. 업데이트 섹션을 확인하십시오. – IamaC

+0

'AddNewConfigPropertyExecute()'에는 두 개의'AllConfig.Add()'가 있습니다. 아마도 그게 문제입니다. 추 신 : 빠른 확인을 위해'Console.Write()'를 사용합니다. – LPL

+0

여분의 AllConfig.Add()를 드려 죄송합니다. 나는 그것을 없애 버렸다. 하지만 내 문제를 해결하지 못했습니다. 필자는 모든 엔티티 프레임 워크 항목을 제외하고 축소 프로젝트를 만들었습니다. 그러나 여전히 같은 문제가 있습니다. 행을 추가하고 편집하고 스크롤하려고 할 때만 발생합니다. 컬렉션에 문제가 발생했습니다. 무엇을 모르겠다. 원한다면 코드를 보낼 수 있습니다. – IamaC

관련 문제