UI不会更新ExpandoObject列表

Lea*_*ner 5 c# wpf user-interface inotifypropertychanged expandoobject

我已经按照实施动态的数据网格这个链接.

我正在使用Converter来绑定值ExpandoObject.列显示了学校总单位等值.

Item      ItemCount DefaultSchool School1  School2 School3

X-Item    200       100           50       50      0
Run Code Online (Sandbox Code Playgroud)

学校可以随时动态添加.现在,如果我将School4添加到40个单位,我想从默认学校中扣除相同的内容(DefaultSchool = 60,School4 = 40).

我可以在转换器中进行此计算,而ItemsSource也会显示更新的值,但它不会反映在UI上.

我使用TextBox的LostFocus事件MyDataGrid.Items.Refresh,它确实更新了UI,但每次失去焦点时,UI也会闪烁,就像一个刷新的网页一样.

我只需要更新当前行.在我使用时ExpandoObject,我不能使用INotifyPropertyChanged(我相信?),那么在这种情况下最好的方法应该是什么?

那么我该如何更新UI呢?

Adi*_*ter 1

我相信这不是ExpandoObject不实施的问题INotifyPropertyChanged(因为它确实如此)。

INotifyCollectionChanged我的想法是你的问题是转换器的组合和使用。发生的情况是,当属性更改时会调用转换器,但当集合更改时不会调用它们。这将导致在绑定集合中添加或删除项目时 UI 不会更新。

您可以查看这些问题以获取有关此问题的更多信息:

您可以通过在转换器中设置断点来查看这是否确实是您的问题,并查看在添加新项目时是否调用它。如果这确实是问题所在,您可以尝试在不使用转换器的情况下解决此问题,或者使用也MultiValueConverter将接收该Count属性的属性(仅充当触发器),如下所示:

<DataGrid>
    <DataGrid.ItemsSource>
        <MultiBinding Converter="{local:MyConverter}">
            <Binding Path="Items" />
            <Binding Path="Items.Count" />
        </MultiBinding>
    </DataGrid.ItemsSource>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
public class MyConverter : MarkupExtension, IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // Your converter logic which will use values[0] (the bound collection).

        // Ignore anything else in the values[] array.
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}
Run Code Online (Sandbox Code Playgroud)