本文介绍了如何以编程方式从另一个DataGridColumn绑定宽度DataGridColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式将一个表中的列的宽度绑定到另一张表中的列的宽度。我该怎么做?这是我的代码:

I'm trying to programmatically binding the width of a column from one table to the width of a column in another table. How can I do it? Here is my code:

DataGridTextColumn classNameColumn = new DataGridTextColumn();
classNameColumn.Header = "CLASS";
classNameColumn.Binding = new Binding("CLASS_NAME");
classNameColumn.IsReadOnly = true;
ScheduleGrid.Columns.Add(classNameColumn);

DataGridTextColumn noneColumn = new DataGridTextColumn();
noneColumn.Header = "";
var bindingWidth = new Binding("Width") { Source = classNameColumn, Mode = BindingMode.TwoWay };
BindingOperations.SetBinding(noneColumn, WidthProperty, bindingWidth);
WeeekHeaderGrid.Columns.Add(noneColumn);

在XAML代码中可以正常工作:

In XAML-code this work fine:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <DataGrid x:Name="ScheduleGrid" Grid.Row="1" AutoGenerateColumns="False" SelectionMode="Single"
              CanUserReorderColumns="False" CanUserAddRows="False" CanUserSortColumns="False"
              CellEditEnding="ScheduleGrid_CellEditEnding">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="ClassName" Header="CLASS" Binding="{Binding CLASS_NAME}" />
        </DataGrid.Columns>
    </DataGrid>
    <DataGrid x:Name="WeeekHeaderGrid" Grid.Row="0" CanUserReorderColumns="False" CanUserAddRows="False" CanUserSortColumns="False" CanUserResizeColumns="True">
        <DataGrid.Columns>
            <DataGridTextColumn Header="" Width="{Binding Source={x:Reference ClassName}, Path=Width, Mode=TwoWay}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>


推荐答案

您使用的是错误的 WidthProperty 代码中的DP。

you are using incorrect WidthProperty DP in code.

将其更改为

BindingOperations.SetBinding(noneColumn, DataGridColumn.WidthProperty, bindingWidth);

DataGridColumn.WidthProperty 具有类型 DataGridLength

这篇关于如何以编程方式从另一个DataGridColumn绑定宽度DataGridColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 10:16