开发者

resizing the last column of a datagrid

开发者 https://www.devze.com 2023-03-16 17:00 出处:网络
I have a DataGrid that is used in two different views. In each case, I would like to have the last column resize it\'s width if the user resizes the host control/view.

I have a DataGrid that is used in two different views. In each case, I would like to have the last column resize it's width if the user resizes the host control/view.

How would you do that?

Cheers,

Berryl

... CanUserResizeColumns="True" >

    <DataGrid.Columns>
        <DataGridTextColumn 
            Header="Number" Binding="{Binding BusinessId}" IsReadOnly="True" 
            CanUserSort="True" CanUserResize="False"
            Width="75"/>
        <DataGridTextColumn 
            Header="Description" Binding="{Binding Description}" IsReadOnly="True" 
            CanUserSort="True" SortDirection="Ascending" CanUserResize="True"
            MinWidth="260" Width="Auto"  />
    </DataGrid.Columns>

</DataGrid>

UPDATE (Working Code)

I just named the column in the xaml and put the following code into the code-behind. If anyone has got a better idea or a way to optimize this, please let me know!

public partial clas开发者_JAVA百科s Listing : UserControl
{
    private double _currentColumnWidth;

    public Listing()
    {
        InitializeComponent();

        Loaded += OnLoaded;
        SizeChanged += OnSizeChanged;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        _currentColumnWidth = colDescription.ActualWidth;
    }

    private void OnSizeChanged(object sender, SizeChangedEventArgs e)
    {
        // split if control is not loaded yet
        if (_currentColumnWidth == 0) return;

        // only interested in width, not height
        var widthChanged = e.WidthChanged;
        if (!widthChanged) return;

        var delta = e.NewSize.Width - e.PreviousSize.Width;
        var newWidth = _currentColumnWidth + delta;
        if (newWidth <= colDescription.MinWidth || newWidth >= colDescription.MaxWidth) return;

        _currentColumnWidth = newWidth;
        colDescription.Width = new DataGridLength(_currentColumnWidth);
    }
}


Easy, just replace the width property in your XAML

<DataGridTextColumn 
                Header="Description" Binding="{Binding Description}" 
                IsReadOnly="True" 
                CanUserSort="True" SortDirection="Ascending" CanUserResize="True"
                MinWidth="260" Width="Auto"  />

to ...

<DataGridTextColumn 
                Header="Description" Binding="{Binding Description}" IsReadOnly="True" 
                CanUserSort="True" SortDirection="Ascending" CanUserResize="True"
                MinWidth="260" Width="*"  />

You do not require any code behind to do any handling of the width as WPF caters for this in XAML.

the "*" indicates an AutoSize value :)


I simplified the code from my original posting 'update' and fixed a bug that was in it. Jason's suggestion to use "*" sizing is simpler and so should be used when a grid is not itself inside of a UserControl, but I am not aware of a better way to keep the column sized properly when it is inside a UserControl.

Cheers,
Berryl

The code below assumes there is a named DataGrid (ie, dgProject) and a named column (ie, colDescription) that needs to be resized when the control is resized.

public partial class Listing : UserControl
{

    public Listing()
    {
        InitializeComponent();

        SizeChanged += OnSizeChanged;
    }

    private void OnSizeChanged(object sender, SizeChangedEventArgs e)
    {
        // split if control is not loaded yet
        if (!dgProject.IsLoaded) return;

        // only interested in width, not height
        if (!e.WidthChanged) return;

        var delta = e.NewSize.Width - e.PreviousSize.Width;
        var newWidth = colDescription.ActualWidth + delta;
        if (newWidth <= colDescription.MinWidth || newWidth >= colDescription.MaxWidth) return;

        colDescription.Width = new DataGridLength(newWidth);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号