How to resize C# WPF Program to fit any screen resolution

点点圈 提交于 2019-12-08 03:15:00

问题


I am trying to figure out how to resize my C# WPF Program to essentially fit any screen resolution. I am trying to fit my program to a computer with a screen resolution of 1280 x 1024. I can't find any information on how to do this exactly, of course I could rig the dimensions of the program to fit the specific screen but every computers screen resolution could be potentially different right? I tried using the obvious solution that was provided here: Resize WPF Window and contents depening on screen resolution but it did not help at all.

i.e. I used: (Also, I wasn't sure if I needed to set the HorizontalAlignment & VerticalAlignment to "stretch" as well, but I did anyway.)

Title="my screen" Height="{Binding SystemParameters.PrimaryScreenHeight}" Width="{Binding SystemParameters.PrimaryScreenWidth}" WindowStartupLocation="CenterScreen" WindowState="Maximized">
    <Grid Name="myGrid" Background="LightGray" Margin="0,0,2,-30" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <TextBox Name="stuffTB" HorizontalAlignment="Left" Height="28" Margin="751,252,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="186" />

...

Whenever I run my exe on the computer with the dimensions of 1280 x 1024 it cuts off contents that are in my grid. Any advice or help would be great, even links to examples would be awesome. Thanks in advance.


回答1:


If you really want to force resize all the controls in the windows you can use ViewBox https://msdn.microsoft.com/pl-pl/library/system.windows.controls.viewbox%28v=vs.110%29.aspx

But IMO you're doing it wrong. The basics of designing good WPF UI would be:

  1. Use grid columns and rows if you want something to hold some part/percentage of the window despite the content
  2. Use DockPanels, StackPanels, WrapPanels etc. eto arrange the UI
  3. Use ScrollViewer to be able to see the controls if they have some minimal dimensions set and on the specific resolution they would get cut off

Look at this sample I've created for you

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="100" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Grid.ColumnSpan="2" Background="Blue" Orientation="Horizontal">
            <Button>Button</Button>
            <Button>Button</Button>
            <Button>Button</Button>
        </StackPanel>
        <DockPanel Grid.Row="1" Grid.Column="1">
            <Rectangle DockPanel.Dock="Right" Width="100" Fill="Red"></Rectangle>
            <Ellipse DockPanel.Dock="Left" Fill="AliceBlue"></Ellipse>
        </DockPanel>
        <ScrollViewer Grid.Row="1">
            <StackPanel>
                <Button Height="200"></Button>
                <Button Height="200"></Button>
                <Button Height="200"></Button>
                <Button Height="200"></Button>
                <Button Height="200"></Button>
                <Button Height="200"></Button>

                <Button Height="200"></Button>
                <Button Height="200"></Button>
            </StackPanel>
        </ScrollViewer>
    </Grid>


来源:https://stackoverflow.com/questions/33847852/how-to-resize-c-sharp-wpf-program-to-fit-any-screen-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!