WPF DataGrid“单元格式等效项”

发布于 2024-11-01 04:40:48 字数 717 浏览 5 评论 0原文

我正在尝试从 WinForms 转换为 WPF,并且在使用 DataGridView -> 时遇到了一些困难WPF 数据网格。

我已经很好地加载了所有数据,只是停留在单元格格式上。

有一个包含数字的列,但是如果数字为零,而不是显示 0,我希望它显示 NIL。

使用 DataGridView,下面的方法有效

    Private Sub SampleDGV_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles SampleDGV.CellFormatting

    Try
        If e.RowIndex >= 0 And e.ColumnIndex = SampleDGV.Columns(CostColumn.Name).Index Then
            If CDec(e.Value.ToString) = 0 Then
                e.Value = "NIL"
                e.FormattingApplied = True
            End If
        End If
    Catch ex As Exception

    End Try


End Sub

但现在我对 WPF 的等效项感到困惑

I'm trying to convert from WinForms to WPF and am struggling a bit with DataGridView -> WPF DataGrid.

I've got all the data loading nicely, just stuck on the cell formatting.

There is a column with numbers, however if the number is zero, rather than show 0, I'd like it to display NIL.

With DataGridView, the below worked

    Private Sub SampleDGV_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles SampleDGV.CellFormatting

    Try
        If e.RowIndex >= 0 And e.ColumnIndex = SampleDGV.Columns(CostColumn.Name).Index Then
            If CDec(e.Value.ToString) = 0 Then
                e.Value = "NIL"
                e.FormattingApplied = True
            End If
        End If
    Catch ex As Exception

    End Try


End Sub

But now I'm stumped on the WPF equivalent

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生活了然无味 2024-11-08 04:40:48

我不确定是否有一种简单的方法可以做到这一点,我会这样做,这似乎确实有效:

使用自定义转换器定义自定义列:

<DataGrid.Resources>
    <local:ZeroToNilConverter x:Key="ZeroToNilConverter"/>
</DataGrid.Resources>
<DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Id, Converter={StaticResource ZeroToNilConverter}}" Header="Test"/>
</DataGrid.Columns>
//Sorry, i do not speak VB...
public class ZeroToNilConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int input = 0;
        try
        {
            input = (int)value;
        }
        catch (Exception)
        {
            return value;
        }

        if (input == 0) return "NIL";
        else return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string input = value as string;
        if (input != null)
        {
            if (input == "NIL") return 0;
            else return value;
        }
        else
        {
            return value;
        }
    }
}

I am not sure if there is an easy way to do this, i'd do it like this which does seem to work:

Define a custom column with a custom converter:

<DataGrid.Resources>
    <local:ZeroToNilConverter x:Key="ZeroToNilConverter"/>
</DataGrid.Resources>
<DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Id, Converter={StaticResource ZeroToNilConverter}}" Header="Test"/>
</DataGrid.Columns>
//Sorry, i do not speak VB...
public class ZeroToNilConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int input = 0;
        try
        {
            input = (int)value;
        }
        catch (Exception)
        {
            return value;
        }

        if (input == 0) return "NIL";
        else return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string input = value as string;
        if (input != null)
        {
            if (input == "NIL") return 0;
            else return value;
        }
        else
        {
            return value;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文