0

我有两个网格,如下所示

<DataGrid ScrollViewer.CanContentScroll="False" GridLinesVisibility="All" VerticalGridLinesBrush="#bdced4" HorizontalGridLinesBrush="#bdced4" 
                RowHeaderWidth="41" HeadersVisibility="All" x:Name="MonitorListHeader" Loaded="MonitorList_Loaded" LoadingRow="dg_LoadingRow" 
                CanUserAddRows="False" CanUserResizeRows="False" 
                CanUserReorderColumns="False" SelectionUnit="Cell" HorizontalAlignment="Stretch" AutoGenerateColumns="false"
                HorizontalScrollBarVisibility="Hidden" >
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="75" Header="ID">
                </DataGridTemplateColumn>

                <DataGridTemplateColumn 
                    Width="{Binding ElementName=CableColumn, Path=ActualWidth}" 
                    Header="Cable">
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Width="*" Header="Port/IP">
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Width="*" Header="Status">
                </DataGridTemplateColumn>    
            </DataGrid.Columns>                
        </DataGrid>

<DataGrid ScrollViewer.CanContentScroll="False" GridLinesVisibility="All" VerticalGridLinesBrush="#bdced4" HorizontalGridLinesBrush="#bdced4" 
                RowHeaderWidth="41" HeadersVisibility="All" x:Name="MonitorList" Loaded="MonitorList_Loaded" LoadingRow="dg_LoadingRow" 
                CanUserAddRows="False" CanUserResizeRows="False" 
                CanUserReorderColumns="False" SelectionUnit="Cell" HorizontalAlignment="Stretch" AutoGenerateColumns="false"
                HorizontalScrollBarVisibility="Hidden" >
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="75" Header="ID">
                </DataGridTemplateColumn>

                <DataGridTemplateColumn x:Name="CableColumn"
                    Width="*" Header="Cable">
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Width="*" Header="Port/IP">
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Width="*" Header="Status">
                </DataGridTemplateColumn>    
            </DataGrid.Columns>                
        </DataGrid>

现在我想将第 2 个网格的第 3 列的标题宽度与第 1 个网格的第 3 列的标题宽度绑定。

谁能告诉我这个绑定代码?

谢谢 !!!!!!

4

1 回答 1

1

我重命名了元素以使其更清晰:

XAML:

... First DataGrid ...
<DataGridTemplateColumn Width="*" Header="Cable" x:Name="SourceColumn" />
...

...Second DataGrid ...
<DataGridTemplateColumn Width="*" Header="Cable" x:Name="TargetColumn" />
...

后面的代码(在构造函数中,就在 之后InitializeComponent();):

var binding = new Binding("Width");
binding.Source = SourceColumn;
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(TargetColumn, DataGridColumn.WidthProperty, binding);

我寻找一个纯 XAML 解决方案,但我没有工作。

于 2013-09-19T11:48:10.440 回答