有没有办法将网格列定义的宽度绑定到数据网格列。这是我的Xaml的一部分。现在它的硬编码如果数据网格列扩展我的网格不会随之移动所以我想将列定义宽度绑定到datagrid列宽。有没有办法做到这一点..
<StackPanel Grid.Row="2" Grid.ColumnSpan="2">
<Grid Margin="48" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="104*"></ColumnDefinition>
<ColumnDefinition Width="165*"></ColumnDefinition>
<ColumnDefinition Width="166*"></ColumnDefinition>
<ColumnDefinition Width="163*"></ColumnDefinition>
<ColumnDefinition Width="58*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="1" HorizontalAlignment="Center" FontWeight="Bold" FontSize="16" Margin="53,0,50,0" Width="62">Extract</Label>
<Label Grid.Column="2" HorizontalAlignment="Center" FontWeight="Bold" FontSize="16" Margin="49,0,29,0" Width="88">Transform</Label>
<Label Grid.Column="3" HorizontalAlignment="Center" FontWeight="Bold" FontSize="16" Margin="59,0,58,0" Width="46">Load</Label>
</Grid>
<DataGrid Name="dgEntities" Height="209" Margin="80,-52,55,-8">
<DataGrid.Columns>
<DataGridTextColumn Header="Entity" Width="70"/>
<DataGridTextColumn Header="Status" Width="90"/>
<DataGridCheckBoxColumn Binding="{Binding IsSelected}" IsThreeState="True" Width="75">
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate x:Name="dtAllChkBx">
<CheckBox Name="cbxAll">Checked</CheckBox>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
<DataGridTextColumn Header="Status" Width="90"></DataGridTextColumn>
<DataGridCheckBoxColumn Binding="{Binding IsSelected}" IsThreeState="True" Width="75">
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate x:Name="dtAllChkTransform">
<CheckBox Name="cbxAllTransform">Checked</CheckBox>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
<DataGridTextColumn Header="Status" Width="90"></DataGridTextColumn>
<DataGridCheckBoxColumn Binding="{Binding IsSelected}" IsThreeState="True" Width="75">
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate x:Name="dtAllChkLoad">
<CheckBox Name="cbxAllLoad">Checked</CheckBox>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:1)
我可以在这里看到两种可能性来回答你的问题:
DataGridColumn
的宽度设置为与Grid.ColumnDefinition
的宽度相同。例如
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
</Grid>
<DataGrid .........>
<DataGrid.Columns>
<DataGridTextColumn Width="5*" ......./>
<DataGridTextColumn Width="4*" ......./>
<DataGridTextColumn Width="3*" ......./>
<DataGridTextColumn Width="2*" ......./>
</DataGrid.Columns>
</DataGrid>
binding
........ e.g。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="c1" Width="5*" />
<ColumnDefinition x:Name="c2" Width="4*" />
<ColumnDefinition x:Name="c3" Width="3*" />
<ColumnDefinition x:Name="c4" Width="2*" />
</Grid.ColumnDefinitions>
</Grid>
<DataGrid .........>
<DataGrid.Columns>
<DataGridTextColumn Width="{Binding Width, ElementName=c1}" ......./>
<DataGridTextColumn Width="{Binding Width, ElementName=c2}" ......./>
<DataGridTextColumn Width="{Binding Width, ElementName=c3}" ......./>
<DataGridTextColumn Width="{Binding Width, ElementName=c4}" ......./>
</DataGrid.Columns>
</DataGrid>
如果第二个解决方案似乎不起作用,那么请尝试Width="{Binding ActualWidth, ElementName=c1}"
而不是Width="{Binding Width, ElementName=c1}"
等等......