WPF DataGrid保存列宽

pdtvr36n  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(111)

我想保存和恢复DataGrid列宽度,我试过这个:

private void dataGrid_LayoutUpdated(object sender, EventArgs e)
{
    foreach (var item in dataGrid.Columns.GetEnumeratorWithIndex())
    {
        Settings.ColumnWidth[item.Index] =
            new DataGridLength(item.Value.Width.Value,
            DataGridLengthUnitType.Pixel,
            item.Value.Width.DesiredValue, item.Value.Width.DisplayValue);
    }
}

字符串
恢复:

foreach (var item in dataGrid.Columns.GetEnumeratorWithIndex())
{
    item.Value.Width = new
        DataGridLength(Settings.ColumnWidth[item.Index].Value,
        DataGridLengthUnitType.Pixel,
        Settings.ColumnWidth[item.Index].DesiredValue,
        Settings.ColumnWidth[item.Index].DisplayValue);
}


但不工作,所有列都存储为“自动”。
更新:


的数据

w46czmvw

w46czmvw1#

尝试使用属性ActualWidth

for (int index = 0; index < dataGrid.Columns.Count; index++)
    Settings.ColumnWidth[index] = new
        DataGridLength(dataGrid.Columns[index].ActualWidth);

字符串
阅读列大小的示例:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void GetWidthsClick(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(string.Join("; ",
            dataGrid.Columns.Select(column => column.Width))
            + Environment.NewLine + string.Join("; ",
            dataGrid.Columns.Select(column => column.ActualWidth)));
    }
}
public class TextsViewModel
{
    public ObservableCollection<Texts> Rows { get; } =
        new ObservableCollection<Texts>()
    {
        new Texts() {Text1="123", Text2="45678",
            Text3="90qwer", Text4="tyuiio", Text5="as"},
        new Texts() {Text1="asad", Text2="gfg",
            Text3="hkjhk", Text4="kl", Text5="fgfhgf"}
    };
}
public class Texts
{
    public string Text1 { get; set; }
    public string Text2 { get; set; }
    public string Text3 { get; set; }
    public string Text4 { get; set; }
    public string Text5 { get; set; }
}


XAML:

<Grid DataContext="{DynamicResource viewModel}">
    <FrameworkElement.Resources>
        <local:TextsViewModel x:Key="viewModel"/>
    </FrameworkElement.Resources>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <DataGrid x:Name="dataGrid" ItemsSource="{Binding Rows}"
        HorizontalAlignment="Center"/>
    <Button Content="Get Widths" Grid.Row="1"
            Click="GetWidthsClick"
            Margin="5" Padding="15 5"/>
</Grid>

relj7zay

relj7zay2#

创建列属性的字典宽度,并在需要时恢复它们:

public partial class MainWindow : Window
{
    Dictionary<int, DataGridLength> columnsWidth = new Dictionary<int, DataGridLength>(); 
    //Dictionary<int, GridViewLength> columnsWidth = new Dictionary<int, GridViewLength>(); //telerik
    public MainWindow()
    {
        InitializeComponent();
        for (int i = 0; i < dataGrid.Columns.Count; i++)
        {
           columnsWidth.Add(i, dataGrid.Columns[i].Width);
        }
    }

    private void RestoreColumnsWidth()
    {
        for (int i = 0; i < dataGrid.Columns.Count; i++)
        {
           dataGrid.Columns[i].Width = columnsWidth[i];
        }
    }
 }

字符串

相关问题