开发者

Managing Application height in WPF (Expressing height as a percentage of screen height)

开发者 https://www.devze.com 2023-03-19 21:08 出处:网络
So I created a WPF application on my monitor which has bigger height (1280 vs 1024) resolution. It is working fine on my monitor.

So I created a WPF application on my monitor which has bigger height (1280 vs 1024) resolution. It is working fine on my monitor.

But when I take it to a monitor of different resolution the whole application extends beyond the monitor and as a result is not usable.

Any idea 开发者_运维问答how we can rectify this. All the height of my controls and window height is fixed


This is easy. Reduce the height of your window. Why are you using a large fixed height window? Obviously I don't know the design of your application, but as you have quickly found out, fixed heights (and widths) tend not to be portable across different pieces of hardware.

The real solution for you is probably to use a WPF Grid control. All of your controls should be in the grid, which fills the entire window. Depending on how you set up your grid rows and columns, your controls will scale in size as the size of your window changes.

Say you have four controls you want evenly stacked in your grid. You could do this:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height=".25*"/>
        <RowDefinition Height=".25*"/>
        <RowDefinition Height=".25*"/>
        <RowDefinition Height=".25*"/>
    </Grid.RowDefintions>
    <MyControl Grid.Row="0"/>
    <MyControl Grid.Row="1"/>
    <MyControl Grid.Row="2"/>
    <MyControl Grid.Row="3"/>
</Grid>

Now each control takes 25% of the available space, and WPF handles everything as your window size changes. You can do much more complicated layouts than this, but hopefully this gets you started.


WPF wasn't really designed to be treated like WinForms, where by absolute control widths, heights, and positions cause these situations. It was designed to be fluid, allow controls to wrap (see WrapPanel), Expand and Collapse (see the Auto or * sizes). If you treat it like WinForms, you'll hit these problems and have to come up with complex workarounds. WPF was designed as the permanent solution to those complex workarounds (amongst other things)

Try looking for a basics tutorial for WPF, to get an idea of how the UI should be layed out.

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号