Bind DataGrid Column Width to Two Colums of Another DataGrid

丶灬走出姿态 提交于 2019-12-01 21:32:33

The code finally executes as expected with the following:

  • <Binding Source="{x:Reference Samp2}" Path="ActualWidth" />
  • return new DataGridLength(totalWidth);

The Converter gets called on load and when resizing Samp1 or Samp2. The column widths remain synchronized as expected.

<DataGridTextColumn x:Name="StatName"  Binding="{Binding a}" Header="Stat">
   <DataGridTextColumn.Width >
       <MultiBinding Converter="{StaticResource WidthConverter}">
           <Binding Source="{x:Reference Samp1}" Path="ActualWidth" />
           <Binding Source="{x:Reference Samp2}" Path="ActualWidth" />
       </MultiBinding>
   </DataGridTextColumn.Width>
</DataGridTextColumn>

The Convert function needed to return a DataGridLength, the data type of DataGridTextColumn.Width.

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{

    double totalWidth = 0;

    foreach (double Width in values)
        totalWidth += Width;

    return new DataGridLength(totalWidth);
}

Note: The code executes as expected, regardless of the error Specified cast is not valid.

  1. The Visual Studio designer underlines the entire tag <MultiBinding ... </MultiBinding> in cyan.
  2. The Error List window reports the error "Specified cast is not valid."
  3. While it is displayed as an error, Visual Studio will still build and execute the code.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!