Bind DataGrid Column Width to Two Colums of Another DataGrid

前端 未结 1 1153
借酒劲吻你
借酒劲吻你 2021-01-20 20:17

Question: How do I bind StatName.Width to Samp1.ActualWidth + Samp2.ActualWidth?

Sub-questions:<

相关标签:
1条回答
  • 2021-01-20 20:26

    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.
    0 讨论(0)
提交回复
热议问题