I have a DataGrid and a grid on my WPF application and I want their respected columns be the same size. I thought maybe I use SharedSizeGroup but it seems that DataGrid columns don’t have this property (Am I wrong?). How can I do this?
Update1
I changed the xaml as follow:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20" MaxHeight="开发者_如何学Go20" MinHeight="20" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid >
<Grid.ColumnDefinitions >
<ColumnDefinition Width="{Binding ElementName=titleDataGridColumn, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=forenameDataGridColumn, Path=ActualWidth}"/>
<ColumnDefinition Width="{Binding ElementName=surnameDataGridColumn, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=dobDataGridColumn, Path=ActualWidth}" />
</Grid.ColumnDefinitions>
<Button Content="Clear" HorizontalAlignment="Stretch" Name="searchClearButton" VerticalAlignment="Stretch" Grid.Column="0" Click="searchClearButton_Click" />
<TextBox HorizontalAlignment="Stretch" Name="forenameTextBox" VerticalAlignment="Stretch" Grid.Column="1" />
<TextBox HorizontalAlignment="Stretch" Name="surnameTextBox" VerticalAlignment="Stretch" Grid.Column="2" />
<TextBox HorizontalAlignment="Stretch" Name="dobTextBox" VerticalAlignment="Stretch" Grid.Column="3" />
</Grid>
<DataGrid Grid.Row="1" x:Name="DataDataGrid" HorizontalAlignment="Stretch" AutoGenerateColumns="False" VerticalAlignment="Stretch" ItemsSource="{Binding}" AlternationCount="1" IsReadOnly="True" SelectionMode="Single" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="titleDataGridColumn" Binding="{Binding Path='Title'}" Header="Title" Width="35*" />
<DataGridTextColumn x:Name="forenameDataGridColumn" Binding="{Binding Path='Forename'}" Header="Forename" Width="65*" />
<DataGridTextColumn x:Name="surenameDataGridColumn" Binding="{Binding Path='Surname'}" Header="Surname" Width="65*"/>
<DataGridTextColumn x:Name="dobDataGridColumn" Binding="{Binding DOB,StringFormat={}\{0:dd/MM/yyyy\}}" Header="DOB" Width="70*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
But now the surname column in grid is bigger than its corresponding column in datagrid and the button is a bit to left ( could be fixed by setting margin on grid column) but I cannot understand why surname columns are not same size?
Update 2
This is working (Problem was the surenameDataGridColumn was not correct (spelling))
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20" MaxHeight="20" MinHeight="20" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid >
<Grid.ColumnDefinitions >
<ColumnDefinition Width="6px"/>
<ColumnDefinition Width="{Binding ElementName=titleDataGridColumn, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=forenameDataGridColumn, Path=ActualWidth}"/>
<ColumnDefinition Width="{Binding ElementName=surnameDataGridColumn, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=dobDataGridColumn, Path=ActualWidth}" />
</Grid.ColumnDefinitions>
<Button Content="Clear" HorizontalAlignment="Stretch" Name="searchClearButton" VerticalAlignment="Stretch" Grid.Column="1" Click="searchClearButton_Click" />
<TextBox HorizontalAlignment="Stretch" Name="forenameTextBox" VerticalAlignment="Stretch" Grid.Column="2" />
<TextBox HorizontalAlignment="Stretch" Name="surnameTextBox" VerticalAlignment="Stretch" Grid.Column="3" />
<TextBox HorizontalAlignment="Stretch" Name="dobTextBox" VerticalAlignment="Stretch" Grid.Column="4" />
</Grid>
<DataGrid Grid.Row="1" x:Name="DataGrid" HorizontalAlignment="Stretch" AutoGenerateColumns="False" VerticalAlignment="Stretch" ItemsSource="{Binding}" AlternationCount="1" IsReadOnly="True" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="titleDataGridColumn" Binding="{Binding Path='Title'}" Header="Title" Width="35*" />
<DataGridTextColumn x:Name="forenameDataGridColumn" Binding="{Binding Path='Forename'}" Header="Forename" Width="65*" />
<DataGridTextColumn x:Name="surnameDataGridColumn" Binding="{Binding Path='Surname'}" Header="Surname" Width="65*"/>
<DataGridTextColumn x:Name="dobDataGridColumn" Binding="{Binding DOB,StringFormat={}\{0:dd/MM/yyyy\}}" Header="DOB" Width="70*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
By adding a new column, I aligned the left of button with the left of Title column in DataGrid.
Use binding to bind the Grid column widths to the size of the columns in your DataGrid. For example:
<dg:DataGridTextColumn x:Name="FirstColumn" Header="Text"/>
<ColumnDefinition Width="{Binding ElementName=FirstColumn, Path=ActualWidth}"/>
精彩评论