简体   繁体   中英

WPF DataGrid Row Header Visibility Error

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行标题错误图像

<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>

尝试设置RowHeaderWidth = 0而不是HeaderVisibility

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM