【问题标题】:How to update row number in DataGrid (WPF)?如何更新 DataGrid (WPF) 中的行号?
【发布时间】:2020-01-27 16:35:01
【问题描述】:

我的DataGrid 看起来像这样

我正是从这里实现的

https://stackoverflow.com/a/15061668/5709159

所以,我有保存转换器

public class RowToIndexConverter : MarkupExtension, IValueConverter
{
    static RowToIndexConverter converter;

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DataGridRow row = value as DataGridRow;
        if (row != null)
            return row.GetIndex();
        else
            return -1;
    }

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

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (converter == null) converter = new RowToIndexConverter();
        return converter;
    }

    public RowToIndexConverter()
    {
    }
}

但是当我删除例如第 2、3、4 行时,问题是什么,所以行的订单数没有更新。 我得到了这样的结果

那么,问题是 - 如何在该行被删除后更新行序号?

【问题讨论】:

标签: c# wpf


【解决方案1】:

所以,最终我在这里找到了解决方案

https://ru.stackoverflow.com/a/853244/195957

首先你需要创建这样的类

public class NumerRow : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        DataGridRow row = values[0] as DataGridRow;
        if (row.DataContext?.GetType().FullName == "MS.Internal.NamedObject") return null;
        return (row.GetIndex() + 1).ToString();
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        => throw new NotSupportedException();
}

.xalm

<DataGridTextColumn Header="№">
    <DataGridTextColumn.Binding>
        <MultiBinding Converter="{StaticResource NumerRow}">
            <Binding Mode="OneWay" RelativeSource="{RelativeSource AncestorType={x:Type DataGridRow}}"/>
            <Binding Mode="OneWay" RelativeSource="{RelativeSource AncestorType={x:Type DataGrid}}" Path="Items.Count"/>
        </MultiBinding>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

结果是

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 2018-12-03
    • 2015-12-04
    • 1970-01-01
    • 2010-11-09
    相关资源
    最近更新 更多