简体   繁体   中英

Width of Datagrid columns WPF

I have a problem with setting a width on my WPF datagrid, I have 2 columns, one for Name and one for Email, I like both to be atleast 100, but if Name is longer, I like it to take as much space as needed. Right now both just end up 100 width no mater how wide the datagrid itself gets. This is how I tried it:

Edited code:

<StackPanel>
    <DataGrid Name="MemberDataGrid" Margin="10,10,10,10" ColumnWidth="*" AutoGenerateColumns="False" ItemsSource="{Binding}" SelectionMode="Single">
        <DataGrid.Columns>
            <DataGridTextColumn MinWidth="30" Width="Auto" Header="Name" Binding="{Binding Path=Name}" />
            <DataGridTextColumn MinWidth="30" Width="*" Header="Email" Binding="{Binding Path=Email}" />
        </DataGrid.Columns>
    </DataGrid>
    <StackPanel Orientation="Horizontal">
        <Button Name="SelectMemeberButton" Click="SelectMemeberButton_Clicked" Margin="10,10,10,10" HorizontalAlignment="Center" Content="Add member"></Button>
        <Button Name="RemoveMemeberButton" Margin="0,10,0,10" HorizontalAlignment="Center" Content="Remove member" Click="RemoveMemeberButton_Clicked"></Button>
    </StackPanel>
</StackPanel>

There's something I'm missing in your problem and I can't see the point. If I look at your window, I have the impression that everything works as it should.

This is the code I used:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new Person[] {
            new Person() { Name = "Paul Leary", Email="paul@contoso.com" },
            new Person() { Name = "Andy Dale", Email="andy@contoso.com" },
            new Person() { Name = "Mike 'The Firestarter' Douglas", Email = "an_incredibly_long_email_address@contoso.com" }
        };

    }
}

And this is the window:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="600">

    <StackPanel Margin="4">
        <DataGrid Name="MemberDataGrid" Margin="10,10,10,10" ColumnWidth="*" AutoGenerateColumns="False" ItemsSource="{Binding}" SelectionMode="Single">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="Col1" MinWidth="100" Width="Auto" Header="Name" Binding="{Binding Path=Name}" />
                <DataGridTextColumn x:Name="Col2" MinWidth="100" Width="*" Header="Email" Binding="{Binding Path=Email}" />
            </DataGrid.Columns>
        </DataGrid>
        <TextBlock Text="{Binding ElementName=Col1, Path=ActualWidth, Mode=OneWay}" Margin="2" />
        <TextBlock Text="{Binding ElementName=Col2, Path=ActualWidth, Mode=OneWay}" Margin="2" />
    </StackPanel>
</Window>

If you run this code you can see that first column is about 164 width. At the same time the second column fits all the remaining space (about 384). So, does this code work in the way that you need?

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