WPF how to make Grid Reevaluate Max Widths on Grid

2019-08-02 13:22发布

问题:

So I have this grid with a bunch of column definitions. The initial column width property is initialized from the settings of the app, which works correctly, then I try to make a max size for the columns so that the user cannot resize the screen so that things don't appear correctly.

The multibinding that is used to calculate the max width seems to be working properly. However, the issue I have is that the multibinding does not Recalculate the value of MaxWidth when a gridsplitter changes the values of the actual width. I have a breakpoint set inside the converter but it is not hit.

However, I don't see how that can be, because according to GridSplitter's documentation:

http://msdn.microsoft.com/en-us/library/system.windows.controls.gridsplitter%28v=vs.110%29.aspx

Resizing with a gridsplitter should change the actualwidth of a columndefinition.

If that is the case, then I would expect the multibinding to update. However, I'm not seeing this.

  <Grid.ColumnDefinitions>
  <ColumnDefinition 
      x:Name="NameListViewColumn"
      MinWidth="100"
      Width="{Binding NameListViewWidth, Source={x:Static DaedalusGraphViewer:SettingsManager.AppSettings}, Mode=OneTime,
                      Converter={StaticResource GridLengthConverter}}"              
      >
    <ColumnDefinition.MaxWidth>
      <MultiBinding Converter="{StaticResource SignalNameColumnMaxSizeConverter}">
        <Binding Path="ActualWidth" 
                 ElementName="NameListViewColumn"
                 />
        <Binding Path="ActualWidth" 
                 ElementName="SignalValuesListViewColumn"
                 />
       <Binding Path="ActualWidth" 
                ElementName="SignalTreeViewColumn"
                />

        <Binding ElementName="SignalValuesListViewColumn" Path="MinWidth" />
        <Binding ElementName="SignalTreeViewColumn" Path="MinWidth" />

      </MultiBinding>
    </ColumnDefinition.MaxWidth>

  </ColumnDefinition>
    <ColumnDefinition Width="Auto" x:Name="SignalNamesGridSpitter"/>
  <ColumnDefinition 
      MinWidth="100"
      x:Name="SignalValuesListViewColumn"
      Width="{Binding SignalValuesListViewWidth, Source={x:Static DaedalusGraphViewer:SettingsManager.AppSettings}, 
                      Mode=OneTime, Converter={StaticResource GridLengthConverter}}"
      >
    <ColumnDefinition.MaxWidth>
      <MultiBinding Converter="{StaticResource SignalValuesMaxSizeConverter}">
        <Binding Path="ActualWidth" 
                 ElementName="SignalValuesListViewColumn"
                 />
        <Binding Path="ActualWidth" 
                 ElementName="SignalTreeViewColumn"
                 />


        <Binding ElementName="SignalTreeViewColumn" Path="MinWidth" />

      </MultiBinding>
    </ColumnDefinition.MaxWidth>
  </ColumnDefinition>
    <ColumnDefinition x:Name="SignalValuesGridSplitter" Width="Auto"/>
    <ColumnDefinition 
      x:Name="SignalTreeViewColumn"
      MinWidth="100"
      Width="*"
      />
    <ColumnDefinition Width="18" MinWidth="18" x:Name="VerticalScrollBarColumn"/>
  </Grid.ColumnDefinitions>

回答1:

Found the answer here: basically the actual width property doesn't implement INotifyPropertyChanged.

Binding to ColumnDefinition.ActualWidth returns 0



回答2:

MaxWidth="{Binding ElementName=SignalValuesGridSplitter,Path=ActualWidth}" TextWrapping="Wrap"/>

hope it helps