2009-08-06 9 views
0

개체 목록에 바인딩 된 Gridview 하위 목록보기 항목이 있습니다. Gridview 아래에 Gridview (Gridview에 바인딩 된)의 내용을 편집 할 수있는 texbox가 있습니다. GridView에 표시되는 새 내용을 추가 할 수 있습니다. 난 내용을 편집 할 때, 그것은 사실 (개체 목록에서) 편집하지만 GRIDVIEW에 표시되지 않습니다 합니다 (의 GridView 업데이트하지 않는 것)WPF Listview + GridView - 업데이트되지 않음보기

XAML 코드 :

<!-- ========= --> 
<!-- root Grid --> 
<!-- ========= --> 
<Grid x:Name="root" Margin="10,10,10,10"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="25" /> 
     <RowDefinition Height="25" /> 
     <RowDefinition Height="40" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="40" /> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 
    <!-- ========= --> 
    <!-- Data Grid --> 
    <!-- ========= --> 
    <ListView x:Name="dataGrid" Grid.Row="0" Grid.ColumnSpan="2" ItemsSource="{Binding}"> 
     <ListView.View> 
      <GridView> 
       <!-- first solution --> 
       <GridViewColumn x:Name="gridColumnName" Header="Name" Width="160"> 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <ContentControl Content="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
       <!-- second solution --> 
       <GridViewColumn x:Name="gridColumnPath" Header="Path" DisplayMemberBinding="{Binding Path=Path}" Width="490" /> 
      </GridView> 
     </ListView.View> 
    </ListView> 

    <!-- ========= --> 
    <!-- Edit Menu --> 
    <!-- ========= --> 
    <Label Content="Name:" Grid.Row="1" Grid.Column="0" VerticalAlignment="Bottom" HorizontalAlignment="Left"/> 
    <TextBox x:Name="txtBoxName" Grid.Row="1" Grid.Column="1" Width="250" VerticalAlignment="Bottom" HorizontalAlignment="Left" 
      DataContext="{Binding ElementName=dataGrid, Path=SelectedItem}" 
      Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 

    <Label Content="Path:" Grid.Row="2" Grid.Column="0" VerticalAlignment="Bottom" HorizontalAlignment="Left" /> 
    <TextBox x:Name="txtBoxPath" Grid.Row="2" Grid.Column="1" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" 
      DataContext="{Binding ElementName=dataGrid, Path=SelectedItem}" 
      Text="{Binding Path=Path, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 

개체 List 클래스 :

class ItemList : ObservableCollection<LdapItem> 
{ 
    public ItemList() 
     : base() 
    { 
    } 
} 

Object 클래스 :

class LdapItem : INotifyPropertyChanged 
{ 
    #region constructor 

    public LdapItem(String name, String path) 
    { 
     this.iD = Guid.NewGuid().ToString(); 
     this.name = name; 
     this.path = path; 
    } 

    #endregion 

    #region public proterties 

    public String ID 
    { 
     get { return iD; } 
    } 

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

    public String Path 
    { 
     get { return path; } 
     set { path = value; } 
    } 

    #endregion 

    #region public methods 

    public void OnPropertyChanged(string prop) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
    } 

    #endregion 

    #region private variables 

    private String name = String.Empty; 
    private String path = String.Empty; 
    private String iD = String.Empty; 

    #endregion 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

어떤 아이디어 왜 updati을 GridView는 작동하지 않습니까?

답변

3

당신이 OnPropertyChangedEvent을 발사하는 것을 잊었다처럼 보인다 때 속성 변경 : 당신이하여 PropertyChanged 이벤트가 발생하지 않는 경우

public String Name 
{ 
    get { return name; } 
    set { 
      name = value; 
      OnPropertyChanged("Name"); 
     } 
} 

, WPF 개체가 변경되었는지 볼 수 없습니다.

4

많은 모델이있는 경우 INPC를 구현하는 기본 클래스를 사용하십시오. 그런 다음 속성 변경 이벤트 처리기를 다음으로 변경하십시오.

public event PropertyChangedEventHandler PropertyChanged; 

public void OnPropertyChanged([CallerMemberName] string propertyName = null) 
{ 
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
} 

이렇게하면 변경중인 모델 속성을 지정하지 않아도됩니다. 맞춤법 오류의 수를 줄이거 나 이름을 잊어 버렸습니다. 몇 가지 설정 도구에서 누락되었지만 여전히 this.OnPropertyChanged()를 호출해야합니다.

ItemList 클래스는 의미가 없습니다. 다음과 같이 대체 할 수 있습니다 :

public ObservableCollection<LdapItem> LdapItems