簡體   English   中英

如何更新 DataGrid (WPF) 中的行號?

[英]How to update row number in DataGrid (WPF)?

我的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 行時,因此行的訂單數沒有更新。 我得到這樣的結果

在此處輸入圖片說明

所以,問題是 - 如何在刪除該行后更新行順序號?

所以,最終我在這里找到了解決方案

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>

結果是

введите сюда описание изображения

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM