c# - 如何绑定(bind)两个 DataGrid 之间的行高?

标签 c# wpf binding datagrid row

我有两个具有相同行数的 DataGrid。但可能是 DataGrid1 中的一行包含更多文本,并且行高比 DataGrid2 中的大。我已经尝试过这样的事情:

 for (int i = 0; i < DataGrid1.Items.Count; i++)
        {
            DataGrid1.ScrollIntoView(DataGrid1.Items[i]);
            DataGridRow row = (DataGridRow)DataGrid1.ItemContainerGenerator.ContainerFromIndex(i);
            Binding bindingHeight = new Binding();
            bindingHeight.Mode = BindingMode.TwoWay;
            bindingHeight.Source = row.ActualHeight;
            bindingHeight.Path = new PropertyPath(DataGridRow.HeightProperty);

            DataGrid2.ScrollIntoView(DataGrid2.Items[i]);
            DataGridRow row2 = (DataGridRow)DataGrid2.ItemContainerGenerator.ContainerFromIndex(i);
            BindingOperations.SetBinding(row2, DataGridRow.HeightProperty, bindingHeight);
        }

关于如何使行具有相同高度的任何想法?

编辑: 问题是我想绑定(bind)单行的行高。 这是它现在的样子: enter image description here 但我希望 DataGrid2 中的特定行具有 DataGrid1 中另一行的行高。所以例如DataGrid2 中 ID 为 12940 + rm 的行与 DataGrid1 中的行具有相同的高度。

最佳答案

您应该在 XAML 中定义绑定(bind):RowHeight="{Binding ElementName=m_DataGrid1, Path=RowHeight}"

或者如果你真的想在后面的代码中绑定(bind):

 m_DataGrid2.SetBinding(DataGrid.RowHeightProperty, new Binding("RowHeight")
            {
                Source = m_DataGrid1
            });

关于c# - 如何绑定(bind)两个 DataGrid 之间的行高?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27502194/

相关文章:

c# - 如何构建嵌入式ActiveMQ Broker?

c# - Roslyn:从代码分析器访问部分类的 XAML

c# - 打开和关闭绑定(bind)

android - 找不到 Mvvmcross MvxListview MvxItemTemplate 资源

c# - 在 Visual Studio 2010 中更改网页后,Designer.cs 损坏

c# - 与此 C# 代码等效的 VB 是什么?

c# - 何时使用闭包的场景

c# - WPF 使用类和子类绑定(bind)到 DataContext

c# - 我什么时候应该在 WPF 应用程序中使用 PixelFormats.Pbgra32?

WPF MVVM 用户控件绑定(bind)问题