跨多个 DataGrid 共享 DataGrid 列宽度

发布于 2024-12-02 23:16:20 字数 490 浏览 0 评论 0原文

我正在一个应用程序中使用相同的列和绑定集创建多个 DataGrid。我希望做的是使所有 DataGrid 对一个 DataGrid 中的更改做出适当的响应。如果我更改一列的大小,其他 DG 中的相应列应具有相同的宽度。这与SO上的这个问题有点相似(WPF share columns width Between alone grids) 除了 DataGrid,而不是 Grid。我希望 DataGrids 有一个类似于 Grid 中的 IsSharedSize 的属性,但情况似乎并非如此。

是否有我可以访问的属性或某种替代方法来完成我想要完成的任务?不过,在有人提出这一点之前,我无法将它们全部合并到一个 DataGrid 中,我正在尝试的意味着由于应用程序本身的性质,我无法将所有信息放入一个 DataGrid 中。

I'm making multiple DataGrids in an application with the same set of columns and bindings. What I'm hoping to do is make all the DataGrid respond appropriately to a change in one DataGrid. If I change the size of one column, the corresponding columns in the other DGs should have the same width. It's somewhat similar to this question on S.O. (WPF share column width between separate grids) except for DataGrids, not Grids. I was hoping DataGrids would have a property similar to IsSharedSize like in Grid but this doesn't seem to be the case.

Is there a property I could access, or some alternative approach, to do what I'm trying to accomplish? Before anyone proposes this though, I cannot merge them all into one DataGrid, what I'm attempting means I can't put all the information in one DataGrid due to the nature of the application itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转角预定愛 2024-12-09 23:16:21

虽然开箱即用的 DataGrid 无法共享宽度,但我发现这是处理此类情况的最佳方法。

在源 DataGrid 列和目标 DataGrid 列宽度之间创建绑定。就我而言,我有两个目标 DataGrid(dgTarget1 和 dgTarget2),因此代码如下:

for (int index = 0; index < dgSource.Columns.Count; index++)
{
    Binding bindingWidth = new Binding();
    bindingWidth.Mode = BindingMode.TwoWay;
    bindingWidth.Source = dgSource.Columns[index];
    bindingWidth.Path = new PropertyPath(DataGridColumn.WidthProperty);
    BindingOperations.SetBinding(dgTarget1.Columns[index], DataGridColumn.WidthProperty, bindingWidth);
    BindingOperations.SetBinding(dgTarget2.Columns[index], DataGridColumn.WidthProperty, bindingWidth);
}   

While sharing width is not possible in DataGrid's out-of-the-box, this is what I came across as the best way to handle such scenarios.

Create bindings between the source DataGrid columns and the target DataGrid columns widths. In my case I have two target DataGrid's (dgTarget1 and dgTarget2), so here is the code:

for (int index = 0; index < dgSource.Columns.Count; index++)
{
    Binding bindingWidth = new Binding();
    bindingWidth.Mode = BindingMode.TwoWay;
    bindingWidth.Source = dgSource.Columns[index];
    bindingWidth.Path = new PropertyPath(DataGridColumn.WidthProperty);
    BindingOperations.SetBinding(dgTarget1.Columns[index], DataGridColumn.WidthProperty, bindingWidth);
    BindingOperations.SetBinding(dgTarget2.Columns[index], DataGridColumn.WidthProperty, bindingWidth);
}   
北方。的韩爷 2024-12-09 23:16:21

在这个聚会上有点晚了,但我遇到了一个类似的场景,我需要一个网格位于 DataGrid 下方并共享相同的列跨度。您可以仅使用 XAML 实现与 digitalguy 的答案类似的功能:

<DataGrid x:Name="dgOne">
    <DataGrid.Columns>
        <DataGridTextColumn Header="One" />
        <DataGridTextColumn Header="Two" />
        <DataGridTextColumn Header="Three" />
        <DataGridTextColumn Header="Four" />
    </DataGrid.Columns>
</DataGrid>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" />
        <ColumnDefinition Width="{Binding Columns[1].ActualWidth, ElementName=dgOne}" />
        <ColumnDefinition Width="{Binding Columns[2].ActualWidth, ElementName=dgOne}" />
        <ColumnDefinition Width="{Binding Columns[3].ActualWidth, ElementName=dgOne}" />
    </Grid.ColumnDefinitions>
    ...
</Grid>

您没有理由不能使用两个 DataGrid 执行相同的操作。

<DataGrid x:Name="dgOne">
    <DataGrid.Columns>
        <DataGridTextColumn Header="One" />
        <DataGridTextColumn Header="Two" />
        <DataGridTextColumn Header="Three" />
        <DataGridTextColumn Header="Four" />
    </DataGrid.Columns>
</DataGrid>
<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Header="One" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" />
        <DataGridTextColumn Header="Two" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" />
        <DataGridTextColumn Header="Three" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" />
        <DataGridTextColumn Header="Four" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" />
    </DataGrid.Columns>
</DataGrid>

A bit late to the party on this one, but I came across a similar scenario where I needed a Grid to sit below a DataGrid and share the same column spans. You can implement something similar to digitguy's answer using just XAML:

<DataGrid x:Name="dgOne">
    <DataGrid.Columns>
        <DataGridTextColumn Header="One" />
        <DataGridTextColumn Header="Two" />
        <DataGridTextColumn Header="Three" />
        <DataGridTextColumn Header="Four" />
    </DataGrid.Columns>
</DataGrid>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" />
        <ColumnDefinition Width="{Binding Columns[1].ActualWidth, ElementName=dgOne}" />
        <ColumnDefinition Width="{Binding Columns[2].ActualWidth, ElementName=dgOne}" />
        <ColumnDefinition Width="{Binding Columns[3].ActualWidth, ElementName=dgOne}" />
    </Grid.ColumnDefinitions>
    ...
</Grid>

There's no reason that you couldn't do the same thing with two DataGrids.

<DataGrid x:Name="dgOne">
    <DataGrid.Columns>
        <DataGridTextColumn Header="One" />
        <DataGridTextColumn Header="Two" />
        <DataGridTextColumn Header="Three" />
        <DataGridTextColumn Header="Four" />
    </DataGrid.Columns>
</DataGrid>
<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Header="One" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" />
        <DataGridTextColumn Header="Two" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" />
        <DataGridTextColumn Header="Three" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" />
        <DataGridTextColumn Header="Four" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" />
    </DataGrid.Columns>
</DataGrid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文