Datagrid - Last column enlarges too much

Asked

Viewed 63 times

0

I have a datagrid displaying data from a Mysql database table. If all columns are Width="Auto", when I resize the window the grid does not follow. If I put in the last column Width="*", it works as I wanted, following the resizing of the window, but opens with the last column excessively wide!

How can I get around this situation?

The code is as follows::

<Window x:Class="DataGridBind.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DataGridBind"
    mc:Ignorable="d"
    Title="DataGridBind" SizeToContent="WidthAndHeight" >
<Grid Background="GhostWhite" >
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <GroupBox Header="Tabela: Produto" Grid.Row="0">
        <StackPanel Orientation="Vertical">
            <DataGrid AutoGenerateColumns="False" Height="Auto" Width="Auto" HorizontalAlignment="Left" Name="dataGridProdutos" ItemsSource="{Binding Path=carregarDados}" CanUserResizeRows="True" AlternatingRowBackground="GhostWhite"  AlternationCount="2" CanUserAddRows="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Path=idProduto}" Header="Código" Width="Auto" IsReadOnly="True" />
                    <DataGridTextColumn Binding="{Binding Path=ProdutoNome}" Header="Nome" Width="Auto" IsReadOnly="True" />
                    <DataGridTextColumn Binding="{Binding Path=ProdutoPU}" Header="Preço Unit." Width="Auto" IsReadOnly="True" />
                    <DataGridTextColumn Binding="{Binding Path=ProdutoStock}" Header="Stock" Width="Auto" IsReadOnly="True" />
                    <DataGridTextColumn Binding="{Binding Path=ProdutoStockMin}" Header="Stock Min." Width="Auto" IsReadOnly="True" />
                    <DataGridTextColumn Binding="{Binding Path=ProdutoStockMax}" Header="Stock Max." Width="Auto" IsReadOnly="True" />
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel>
    </GroupBox>
    <GroupBox Header="Ações" Grid.Row="1" >
        <StackPanel Orientation="Horizontal" Margin="10" HorizontalAlignment="Right">
            <Button Content="Atualizar" Height="25" HorizontalAlignment="Left" Name="btnAtualizar" Width="100" Margin="5" Click="btnAtualizar_Click"/>
            <Button Content="Sair" Height="25" HorizontalAlignment="Left" Name="btnSair" Width="100" Margin="5" Click="btnSair_Click" />
        </StackPanel>
    </GroupBox>
</Grid>

  • source code + database: https://dl.dropboxusercontent.com/u/2715374/WPF_MySQL_01%20v2.zip

1 answer

0

I know what caused this behavior. I had the property Sizetocontent="Widthandheight" in the window. Probably the width of the last column was calculated from the maximum available resolution size!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.