INotifyPropertyChanged用于索引属性

时间:2018-10-30 23:07:47

标签: c# wpf binding datagrid inotifypropertychanged

所以我有一个如下的课程:

public class Data : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public object this[string field]
    {
        get => // gets the value
        set
        {
            // sets the value
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item[]"));
        }
    }
}

在我看来:

<DataGrid
    AutoGenerateColumns="False"
    ItemsSource="{Binding Rows}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="[some_field]" Binding="{Binding Path=[some_field]}" />
        <DataGridTextColumn Header="[some_other_field]" Binding="{Binding Path=[some_other_field]}" />
    </DataGrid.Columns>
</DataGrid>

这很好,但是当发生PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item[]"));时,DataGrid会同时提取"some_field"中的字段"some_other_field"和字段Data,而我没有找到了一种使用PropertyChanged的方法,可以使它仅刷新已更新的字段。

我尝试过"Item[" + field + "]""[" + field + "]"field,但是没有运气。这是可能/受支持的事情,还是我做错了什么?

1 个答案:

答案 0 :(得分:1)

我认为这是不可能的,至少对于WPF和dot NET Framework而言。

建议/解决方法:如果我们向视图模型添加了一些只读属性,该怎么办? 示例:

purrr

还有XAML:

public class Data : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public object this[string field]
    {
        get => // gets the value
        set
        {
            // sets the value
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(field));
        }
    }

    public object some_field => this[nameof(some_field)];
    public object some_other_field=> this[nameof(some_other_field)];
}

并没有真正具体回答您的问题,但是它确实提供了一种控制刷新多少视图的方法。