Alexander Van Atta
Alexander Van Atta

Reputation: 890

Forcing two WPF controls to have the same width while maintaining auto sizing from xaml (Both controls have greater of two width)

I am trying to define a 1x3 grid where the first and third columns widths auto-size to fit their contents but maintain equal widths. I figured I could achieve this by binding each ColumnDefinition's MinWidth property to be the other columns ActualWidth. But it does not seem to work. Can someone explain why the following code does not work?

'''

<DataTemplate DataType="{x:Type edvm:NodeViewModel}">
   <Grid">
      <Grid.ColumnDefinitions>
        <ColumnDefinition
             x:Name="inCol"
             Width="Auto"
             MinWidth="{Binding ElementName=outCol, Path=ActualWidth}" />
        <ColumnDefinition 
            Width="*" 
            MinWidth="25" />
        <ColumnDefinition
            x:Name="outCol"
            Width="Auto"
            MinWidth="{Binding ElementName=inCol, Path=ActualWidth}" />
     </Grid.ColumnDefinitions>
   </Grid>
</DataTemplate>

'''

Upvotes: 0

Views: 798

Answers (1)

Clemens
Clemens

Reputation: 128157

Set the SharedSizeGroup property of both ColumnDefinitions. That would implicitly also set Width="Auto".

<Grid Grid.IsSharedSizeScope="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition SharedSizeGroup="OuterColumns"/>
        <ColumnDefinition MinWidth="25"/>
        <ColumnDefinition SharedSizeGroup="OuterColumns"/>
    </Grid.ColumnDefinitions>
    ...
</Grid>

Upvotes: 4

Related Questions