简体   繁体   中英

WPF Datagrid Column width Resize when maximize

I have WPF data grid added in the Grid Column here is the XAML

<DataGrid x:Name="grdProgramList" IsReadOnly="True" 
          Grid.Column="1" Grid.Row="1" 
          Width="{Binding Path=ActualWidth, ElementName=grid}"
          RenderOptions.ClearTypeHint="Enabled"
          TextOptions.TextFormattingMode="Display"
          HeadersVisibility="All"
          Margin="5"
          SelectionMode="Single"
          SelectionUnit="FullRow"
          SelectionChanged="AzureDataGrid_Selected"
          ItemsSource="{Binding Path=Uninstaller.ProgramCollection}"
          SelectedItem="{Binding Uninstaller.SelectedProgramDetails,  Mode=TwoWay}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="PROGRAM NAME" 
                            Binding="{Binding ProgramDetails.ProgramName}" 
                            MinWidth="325" />
        <DataGridTextColumn Header="Publisher"
                            Binding="{Binding ProgramDetails.PublisherName}" />
        <DataGridTextColumn Header="Estimated Size (KB)"
                            Binding="{Binding ProgramDetails.EstimatedSize}" />
        <DataGridTextColumn Header="Version"
                            Binding="{Binding ProgramDetails.Version}" />
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
            <Style.Triggers>
                <!-- IsSelected -->
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="true" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background" Value="#CC119EDA" />
                    <Setter Property="Foreground" Value="White" />
                    <Setter Property="BorderBrush" Value="#CC119EDA" />
                </MultiDataTrigger>
                <!-- IsHover -->
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background" Value="#66119EDA" />
                    <Setter Property="BorderBrush" Value="#66119EDA" />
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

when it re-sizes i want to fit all the columns width the grid width

right now it looks like this.

在此处输入图片说明

You should set the Width property of the DataGridColumn . It is of type DataGridLength , which allows you to set the values proportional.
You can do this by setting the value to a number followed by a * (eg 2* , * defaults to 1* ).
If you want every column to have the same width , set it to * on each one:

<DataGrid.Columns>
    <DataGridTextColumn Header="PROGRAM NAME" 
                        Binding="{Binding ProgramDetails.ProgramName}" 
                        MinWidth="325" Width="*" />
    <DataGridTextColumn Header="Publisher"
                        Binding="{Binding ProgramDetails.PublisherName}" Width="*" />
    <DataGridTextColumn Header="Estimated Size (KB)"
                        Binding="{Binding ProgramDetails.EstimatedSize}"  Width="*"/>
    <DataGridTextColumn Header="Version"
                        Binding="{Binding ProgramDetails.Version}" Width="*" />
</DataGrid.Columns>

If you only want the last column to take up the remaining space, set the Width only on the one:

 <DataGrid.Columns>
    <DataGridTextColumn Header="PROGRAM NAME" 
                        Binding="{Binding ProgramDetails.ProgramName}" 
                        MinWidth="325" />
    <DataGridTextColumn Header="Publisher"
                        Binding="{Binding ProgramDetails.PublisherName}" />
    <DataGridTextColumn Header="Estimated Size (KB)"
                        Binding="{Binding ProgramDetails.EstimatedSize}" />
    <DataGridTextColumn Header="Version"
                        Binding="{Binding ProgramDetails.Version}" Width="*" />
</DataGrid.Columns>

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