2009-08-06 17 views
0

私はListview ItemをGridview Childに持っていて、List of Objectsにバインドしています。 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)); 
} 

これにより、変更されるモデルプロパティを指定する必要がなくなります。スペルミスの数を減らすか、名前を忘れてしまいます.OnPropertyChanged()を呼び出す必要がありますが、いくつかのセッターでは欠けています。

ItemListクラスは意味がありません。それは次のように置き換えることができます:

public ObservableCollection<LdapItem> LdapItems 
関連する問題