【问题标题】:WPF Listview + GridView - View not UpdatingWPF Listview + GridView - 查看不更新
【发布时间】:2009-08-06 08:33:25
【问题描述】:

我有一个带有 Gridview 子项的 Listview 项,绑定到一个对象列表。 在 Gridview 下面我有 texboxes 来编辑 Gridview 的内容(绑定到 Gridview)。 我可以添加新内容(显示在 GridView 中)。 当我编辑内容时,它实际上已被编辑(在对象列表中)但未显示在 Gridview 中(GridView 似乎没有更新)

xml代码:

<!-- ========= -->
<!-- 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}" />

对象列表类:

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

对象类:

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;
}

任何想法为什么更新 GridView 不起作用?

【问题讨论】:

    标签: c# wpf gridview listview


    【解决方案1】:

    如果您有多个模型,请使用实现 INPC 的基类。然后将您的属性更改事件处理程序更改为:

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

    这将消除指定要更改的模型属性的需要。减少拼写错误或忘记输入名称的次数。仍然需要调用 this.OnPropertyChanged() 虽然您在几个 setter 中都缺少它。

    ItemList 类没有意义。它可以替换为:

    public ObservableCollection<LdapItem> LdapItems 
    

    【讨论】:

      【解决方案2】:

      您似乎忘记在属性更改时触发 OnPropertyChangedEvent:

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

      如果不触发 PropertyChanged 事件,WPF 将无法查看对象是否已更改。

      【讨论】:

        猜你喜欢
        • 2018-12-19
        • 1970-01-01
        • 2011-06-24
        • 2023-04-10
        • 2016-08-16
        • 2021-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多