4

Is it possible, to link two ViewBoxes, so that they always use the same scale factor? Or is it possible, to extract the current scale factor of a ViewBox to use in a ScaleTransform of another element?

Thank you.

Nostromo
  • 1,177
  • 10
  • 28

1 Answers1

1

I come up with an idea. I think we can leverage the Grid's SharedSizeGroup functionality to accomplish this.

Here is a simple sample.

<ListBox Grid.IsSharedSizeScope="True" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <UniformGrid/>
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
  <ListBox.ItemTemplate>
    <DataTemplate>
      <Viewbox Margin="5" Stretch="Uniform">
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="col"/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition SharedSizeGroup="row"/>
          </Grid.RowDefinitions>
          <TextBlock Text="{Binding Name}"/>
        </Grid>
      </Viewbox>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

public partial class Test2 : Window
{

  public Test2()
  {
    InitializeComponent();
    DataContext = new List<MyClass2>()
    {
      new MyClass2(){Name="A"},
      new MyClass2(){Name="BB"},
      new MyClass2(){Name="CCCCCCCCCCCC"},
      new MyClass2(){Name="DDD"},
      new MyClass2(){Name="EEEEEEE"}
    };
  }
}

class MyClass2
{
  public string Name { get; set; }
}
Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77