Click here to Skip to main content
15,891,372 members
Articles / Desktop Programming / WPF

Sharing size between different controls

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
28 Dec 2012MIT1 min read 8.2K   127   4  
This article introduces a simple control which allows to share height and/or width between multiple instances of it.
<Window x:Class="SharedSizeTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sharedSize="clr-namespace:SharedSize;assembly=SharedSizeCtrl"
        xmlns:my="clr-namespace:SharedSizeTestApp"
        Title="Shared Size Control" Height="500" Width="525">

  <Window.Resources>
    <!-- converter to convert bool into Visibility -->
    <my:BoolVisibilityConverter x:Key="BoolVisibilityConverter"/>

    <!-- alternating row background for ListBox -->
    <Style TargetType="{x:Type ListBoxItem}">
      <Style.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
          <Setter Property="Background" Value="#19f39611"></Setter>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
          <Setter Property="Background" Value="#19000000"></Setter>
        </Trigger>
      </Style.Triggers>
    </Style>
  </Window.Resources>
  
  <!-- main layout grid -->
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!-- some stack panels to hold 3x3 SharedSizeControls -->
    <StackPanel Orientation="Horizontal" Grid.Row="0"
                    sharedSize:SharedSizeControl.IsSharedSizeScope="True">
      <StackPanel Orientation="Vertical">
        <!-- each SharedSizeControl is surrounded by a thin border to show the controls exact size -->
        <Border BorderBrush="Red" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top">
          <sharedSize:SharedSizeControl SharedHeightGroup="TopHeight" SharedWidthGroup="LeftWidth">
            
            <!-- the content is a border that does not necessarrily fill the SharedSizeControl -->
            <Border BorderBrush="Black" BorderThickness="2" Background="Red"
                  Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"
                  Width="{Binding ElementName=TopLeftWidthSlider, Path=Value}">
              
              <!-- some contens have a slider to control the content width or height -->
              <Slider Orientation="Horizontal" Width="50" Height="20" Margin="3"
                    HorizontalAlignment="Left"
                    Minimum="70" Maximum="150" Value="100"
                    x:Name="TopLeftWidthSlider"/>
            </Border>
          </sharedSize:SharedSizeControl>
        </Border>

        <Border BorderBrush="Green" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top">
          <sharedSize:SharedSizeControl SharedHeightGroup="MidHeight" SharedWidthGroup="LeftWidth">
            <Border BorderBrush="Black" BorderThickness="2" Background="Green"
                  Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"
                  Width="{Binding ElementName=MidLeftWidthSlider, Path=Value}">
              <Slider Orientation="Horizontal" Width="50" Height="20" Margin="3"
                    HorizontalAlignment="Left"
                    Minimum="70" Maximum="170" Value="80"
                    x:Name="MidLeftWidthSlider"/>
            </Border>
          </sharedSize:SharedSizeControl>
        </Border>

        <Border BorderBrush="Blue" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top">
          <sharedSize:SharedSizeControl SharedHeightGroup="BottomHeight" SharedWidthGroup="LeftWidth">
            <Border BorderBrush="Black" BorderThickness="2" Background="Blue"
                  Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"
                  Width="{Binding ElementName=LowLeftWidthSlider, Path=Value}">
              <Slider Orientation="Horizontal" Width="50" Height="20" Margin="3"
                    HorizontalAlignment="Left"
                    Minimum="70" Maximum="180" Value="70"
                    x:Name="LowLeftWidthSlider"/>
            </Border>
          </sharedSize:SharedSizeControl>
        </Border>
      </StackPanel>
      <StackPanel Orientation="Vertical">
        <Border BorderBrush="Yellow" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top">
          <sharedSize:SharedSizeControl SharedHeightGroup="TopHeight" SharedWidthGroup="MidWidth">
            <Border BorderBrush="Black" BorderThickness="2" Background="Yellow"
                  Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"
                  Height="{Binding ElementName=TopMidHeightSlider, Path=Value}">
              <Slider Orientation="Vertical" Width="20" Height="50" Margin="3"
                    VerticalAlignment="Top"
                    Minimum="70" Maximum="180" Value="70"
                    x:Name="TopMidHeightSlider"/>
            </Border>
          </sharedSize:SharedSizeControl>
        </Border>

        <Border BorderBrush="Silver" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top">
          <sharedSize:SharedSizeControl SharedHeightGroup="MidHeight" SharedWidthGroup="MidWidth">
            <Border BorderBrush="Black" BorderThickness="2" Background="Silver"
                  Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>
          </sharedSize:SharedSizeControl>
        </Border>

        <Border BorderBrush="Silver" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top">
          <sharedSize:SharedSizeControl SharedHeightGroup="BottomHeight" SharedWidthGroup="MidWidth">
            <Border BorderBrush="Black" BorderThickness="2" Background="Silver"
                  Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>
          </sharedSize:SharedSizeControl>
        </Border>
      </StackPanel>
      <StackPanel Orientation="Vertical">
        <Border BorderBrush="Magenta" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top">
          <sharedSize:SharedSizeControl SharedHeightGroup="TopHeight" SharedWidthGroup="RightWidth">
            <Border BorderBrush="Black" BorderThickness="2" Background="Magenta"
                  Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"
                  Height="{Binding ElementName=TopRightHeightSlider, Path=Value}">
              <Slider Orientation="Vertical" Width="20" Height="50" Margin="3"
                    VerticalAlignment="Top"
                    Minimum="70" Maximum="150" Value="80"
                    x:Name="TopRightHeightSlider"/>
            </Border>
          </sharedSize:SharedSizeControl>
        </Border>

        <Border BorderBrush="Silver" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top">
          <sharedSize:SharedSizeControl SharedHeightGroup="MidHeight" SharedWidthGroup="RightWidth">
            <Border BorderBrush="Black" BorderThickness="2" Background="Silver"
                  Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>
          </sharedSize:SharedSizeControl>
        </Border>

        <Border BorderBrush="Silver" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top">
          <sharedSize:SharedSizeControl SharedHeightGroup="BottomHeight" SharedWidthGroup="RightWidth">
            <Border BorderBrush="Black" BorderThickness="2" Background="Silver"
                    Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>
          </sharedSize:SharedSizeControl>
        </Border>
      </StackPanel>
    </StackPanel>

    
    <!-- starting here we have 3 ListBoxes which items of each row share theire height -->
    <!-- by clicking a CheckBox additional info is added to one ListBoix. By this thoe row height changes -->
    <CheckBox Grid.Row="1" Margin="3"
              Content="Show more info"
              x:Name="ShowMoreInfo"/>

    <Grid Grid.Row="2" sharedSize:SharedSizeControl.IsSharedSizeScope="True">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="*"/>
      </Grid.ColumnDefinitions>

      <ListBox Grid.Column="0" AlternationCount="2"
               ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=Items}">
        <ListBox.ItemTemplate>
          <DataTemplate DataType="{x:Type my:ItemType}">
            <sharedSize:SharedSizeControl SharedHeightGroup="{Binding Path=UniqueId, StringFormat='{}{0}_Height'}">
              <TextBlock Text="{Binding Path=Name}" 
                         VerticalAlignment="Top" Margin="3,1"/>
            </sharedSize:SharedSizeControl>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>

      <ListBox Grid.Column="1" AlternationCount="2"
               ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=Items}">
        <ListBox.ItemTemplate>
          <DataTemplate DataType="{x:Type my:ItemType}">
            <sharedSize:SharedSizeControl SharedHeightGroup="{Binding Path=UniqueId, StringFormat='{}{0}_Height'}">
              <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding Path=SomeInfo}" 
                           Margin="3,1"/>
                <TextBlock Text="{Binding Path=SomeMoreInfo}"
                           Margin="3,1"
                           Visibility="{Binding ElementName=ShowMoreInfo, Path=IsChecked, Converter={StaticResource BoolVisibilityConverter}}"/>
              </StackPanel>
            </sharedSize:SharedSizeControl>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>

      <ListBox Grid.Column="2" AlternationCount="2"
               ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=Items}">
        <ListBox.ItemTemplate>
          <DataTemplate DataType="{x:Type my:ItemType}">
            <sharedSize:SharedSizeControl SharedHeightGroup="{Binding Path=UniqueId, StringFormat='{}{0}_Height'}">
              <TextBlock Text="{Binding Path=ExtraInfo}" 
                         VerticalAlignment="Top" Margin="3,1"/>
            </sharedSize:SharedSizeControl>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>

    </Grid>
  </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior) remes GmbH
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions