Search code examples
c#wpfdata-bindinglayouttransform

Databinding to the width of a form


I am trying to dynamically scale my UI of my WPF based application, I'd like to use the ratio of the current resolution and my native resolution using something like this:

    <Grid.LayoutTransform>
        <ScaleTransform CenterX="0" CenterY="0" ScaleX="{Binding FormWidth/NativeREsolution}" ScaleY="{Binding FormWidth/NativeREsolution}"/>
    </Grid.LayoutTransform>

The reason why I found scale transform is it scales all the UI elements within the container, including frames and subpages.

is there anyway to do this?

Alternatively is there a better way to dynamically scale an application depending on the size of the window?


Solution

  • WPF is Resolution Independent by nature.

    <Window ..>
       <Grid>
    
       <!-- Content here -->
    
       </Grid>
    </Window>
    

    The above XAML will cause the Grid to stretch to the Window size. No horrendous winforms-like hacks required.

    Edit:

    if you want Everything (including Font Sizes) to scale within a Window, just use a Viewbox:

    <Window>
       <Viewbox>
           <Grid>
    
           <!-- Content here -->
    
           </Grid>
       </Viewbox>
    </Window>