WPF DataGrid 行标题可见性错误

发布于 2024-12-23 16:03:17 字数 1245 浏览 0 评论 0原文

我正在使用 DataGrid 来显示多个字段,其中之一是多行描述。网格显示数据很好,直到我尝试通过设置 HeadersVisibility="Column" 隐藏标题行。标题行消失,但当我滚动时,某些随机行的行标题会重新出现。

我已将其范围缩小到显示多行描述的列。只要我离开此专栏,那么我就不会遇到这个问题。我尝试用 "\r\n""\n" 分隔行,但都不起作用。 DataGrid 支持多行文本字段吗?

下面的图片显示了正在发生的情况以及我用于创建网格的 XAML。

DataGrid 行标题错误图像

<DataGrid DataContext="{StaticResource personRepository}"
          ItemsSource="{Binding PersonList, Mode=OneWay}"
          AutoGenerateColumns="False"
          HeadersVisibility="Column"
          CanUserSortColumns="False"
          SelectionMode="Extended"
          IsReadOnly="True">

    <DataGrid.Columns>
        <DataGridTextColumn Header="Id" Width="80" Binding="{Binding Id, Mode=OneWay}" />
        <DataGridTextColumn Header="First Name" Width="150" Binding="{Binding FirstName, Mode=OneWay}" />
        <DataGridTextColumn Header="Last Name" Width="150" Binding="{Binding LastName, Mode=OneWay}" />
        <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description, Mode=OneWay}" />
    </DataGrid.Columns>
</DataGrid>

I am using a DataGrid to display several fields, one of which is a multi-line description. The grid displays the data just fine until I try to hide the header rows by setting HeadersVisibility="Column". The header rows disappear but then while I am scrolling the row header reappears for some random rows.

I have narrowed it down to the column that displays multi-line description. As long as I leave this column off, then I don't have this issue. I have tried separating the lines by both "\r\n" and "\n" but neither work. Does the DataGrid support multi-line text fields?

Below is a picture to show what is happening and the XAML I used to create the grid.

DataGrid Row Header Error Image

<DataGrid DataContext="{StaticResource personRepository}"
          ItemsSource="{Binding PersonList, Mode=OneWay}"
          AutoGenerateColumns="False"
          HeadersVisibility="Column"
          CanUserSortColumns="False"
          SelectionMode="Extended"
          IsReadOnly="True">

    <DataGrid.Columns>
        <DataGridTextColumn Header="Id" Width="80" Binding="{Binding Id, Mode=OneWay}" />
        <DataGridTextColumn Header="First Name" Width="150" Binding="{Binding FirstName, Mode=OneWay}" />
        <DataGridTextColumn Header="Last Name" Width="150" Binding="{Binding LastName, Mode=OneWay}" />
        <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description, Mode=OneWay}" />
    </DataGrid.Columns>
</DataGrid>

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

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

发布评论

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

评论(2

梦途 2024-12-30 16:03:17

尝试设置 RowHeaderWidth = 0 而不是 HeaderVisibility

Try setting RowHeaderWidth = 0 instead of HeaderVisibility

黑色毁心梦 2024-12-30 16:03:17

在本例中,lvCurDocFields 是父 ListView。这里的缺点是您需要为其他列设置硬宽度,然后这些其他列的总和就是 ConverterParameter。如果你有一个垂直滚动条,那么留下大约 20 个。GridView 有点痛苦,但我喜欢它的演示文稿,因为只读它比 DataGrid 高效得多

<GridViewColumn Width="{Binding ElementName=lvCurDocFields, Path=ActualWidth, Converter={StaticResource widthConverter}, ConverterParameter=100}">

[ValueConversion(typeof(double), typeof(double))]
public class WidthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // value is the total width available
        double otherWidth;
        try
        {
            otherWidth = System.Convert.ToDouble(parameter);
        }
        catch
        {
            otherWidth = 100;
        }
        if (otherWidth < 0) otherWidth = 0;

        double width = (double)value - otherWidth;
        if (width < 0) width = 0;
        return width; // columnsCount;

    }

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

In this case lvCurDocFields is the parent ListView. The down side here is you need to set hard widths for the other columns and then the total of those other columns is the ConverterParameter. If you have a vertical scroll bar then leave about 20. GridView is kind of a pain but I like the presentation as for read only it is much more efficient than DataGrid

<GridViewColumn Width="{Binding ElementName=lvCurDocFields, Path=ActualWidth, Converter={StaticResource widthConverter}, ConverterParameter=100}">

[ValueConversion(typeof(double), typeof(double))]
public class WidthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // value is the total width available
        double otherWidth;
        try
        {
            otherWidth = System.Convert.ToDouble(parameter);
        }
        catch
        {
            otherWidth = 100;
        }
        if (otherWidth < 0) otherWidth = 0;

        double width = (double)value - otherWidth;
        if (width < 0) width = 0;
        return width; // columnsCount;

    }

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