>我有两个网格,如下所示
<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>
现在我想将第二个网格的第三列的标题宽度与第一个网格的第三列的标题宽度绑定。
任何人都可以告诉我这个的绑定代码吗?
谢谢!!!!!!
我重命名了元素以使其更清晰:
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 解决方案,但我没有工作。