简体   繁体   English

索引属性的 INotifyPropertyChanged

[英]INotifyPropertyChanged for indexed properties

So I have a class as follows:所以我有一个类如下:

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[]"));
        }
    }
}

and in my view :在我看来:

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

This works fine but when PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item[]"));这工作正常,但当PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item[]")); happens, the DataGrid will fetch both the field "some_field" and the field "some_other_field" in Data and I haven't found a way to use the PropertyChanged that would make it refresh only the field that was updated.发生时, DataGrid将获取两个领域"some_field"和领域"some_other_field"Data ,我还没有找到一种方法,使用PropertyChanged这将使它只刷新已更新的领域。

I've tried with "Item[" + field + "]" , "[" + field + "]" , field but no luck.我试过"Item[" + field + "]" , "[" + field + "]" , field但没有运气。 Is this something that is possible/supported or am I doing something wrong?这是可能/支持的事情还是我做错了什么?

I think it's not possible, at least for WPF and dot NET Framework.我认为这是不可能的,至少对于 WPF 和 dot NET Framework。

Suggestion/workaround: What if we added some read-only properties to the view model?建议/解决方法:如果我们向视图模型添加一些只读属性会怎样? Example:例子:

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

And the XAML:和 XAML:

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

Doesn't really answer your question specifically, but it does provide a way to control how much of the view gets refreshed.并没有真正具体回答您的问题,但它确实提供了一种控制视图刷新量的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM