locked
Binding Height & Width to Parent XAML RRS feed

  • Question

  • Hi,

    I'm trying to bind the H & W of a ScrollViewer to that of its parent container.  I've tried using:

    Width="{Binding ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor, TemplateParent}}"

    or

    Height="{Binding ActualHeight,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=FlipView}}">

    But neither is recognized as valid.

    I've also tried binding it directly to the parent as:

    Width="{Binding ActualWidth, ElementName=parentView, Mode=OneWay}"

    Bu this too had no effect.

    Does anyone have any idea how to do this?.

    Also, it seems the property Stretch="Uniform" and Stretch="UniformToFill" have the exact same effect.  Is this a known bug with 8.1, or perhaps something else in the XAML at work here? What should the difference be?

    Thanks!

    Thursday, August 22, 2013 2:28 PM

Answers

  • If you do not want the ScrollViewer to resize for its content, disable scrolling in all directions.  This should work for you.  Just curious, why do you need this root element to be a ScrollViewer, for zooming? 

    <FlipView x:Name="flipView"> <FlipView.ItemTemplate> <DataTemplate> <ScrollViewer HorizontalScrollBarVisibility="Disabled" HorizontalScrollMode="Disabled" VerticalScrollBarVisibility="Disabled" VerticalScrollMode="Disabled"> <Image Source="{Binding}" Stretch="Uniform" /> </ScrollViewer> </DataTemplate> </FlipView.ItemTemplate> </FlipView>


    • Marked as answer by NewToWin8 Friday, August 23, 2013 12:39 PM
    Thursday, August 22, 2013 8:36 PM

All replies

  • What is your parent container?  If you could share a simple piece of XAML that illustrates the problem.

    Also Uniform and UniformToFill seem to be working correctly, try the code below.  If you still see a problem with stretching please start up a new thread on that and we can look deeper.

    <StackPanel>
        <Border Height="300" Width="300" Background="Blue">
            <Viewbox Stretch="Uniform">
                <TextBlock Text="test" />
            </Viewbox>
        </Border>
        <Border Height="300" Width="300" Background="Red">
            <Viewbox Stretch="UniformToFill">
                <TextBlock Text="test" />
            </Viewbox>
        </Border>
    </StackPanel>

           

    Thursday, August 22, 2013 4:08 PM
  • If you do not want the ScrollViewer to resize for its content, disable scrolling in all directions.  This should work for you.  Just curious, why do you need this root element to be a ScrollViewer, for zooming? 

    <FlipView x:Name="flipView"> <FlipView.ItemTemplate> <DataTemplate> <ScrollViewer HorizontalScrollBarVisibility="Disabled" HorizontalScrollMode="Disabled" VerticalScrollBarVisibility="Disabled" VerticalScrollMode="Disabled"> <Image Source="{Binding}" Stretch="Uniform" /> </ScrollViewer> </DataTemplate> </FlipView.ItemTemplate> </FlipView>


    • Marked as answer by NewToWin8 Friday, August 23, 2013 12:39 PM
    Thursday, August 22, 2013 8:36 PM