dataGrid 上的 SharedSizeGroup
我的 WPF 应用程序上有一个 DataGrid 和一个网格,我希望它们所尊重的列具有相同的大小。我想也许我使用 SharedSizeGroup 但似乎 DataGrid 列没有此属性(我错了吗?)。 我该怎么做?
Update1
我更改了 xaml,如下所示:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20" MaxHeight="20" 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>
但现在网格中的姓氏列比数据网格中的相应列大,并且按钮有点偏左(可以通过在网格列上设置边距来修复),但我无法理解为什么姓氏列是尺寸不一样?
更新2
这是工作(问题是surenameDataGridColumn 不正确(拼写))
<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>
通过添加新列,我将按钮的左侧与DataGrid 中标题列的左侧对齐。
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="20" 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用绑定将 Grid 列宽绑定到 DataGrid 中的列大小。例如:
Use binding to bind the Grid column widths to the size of the columns in your DataGrid. For example: