wpf自适应屏幕分辨率:viewbox控件的使用方法

使用View Box:在固定长宽的canvas/grid外添加viewbox控件,窗口大小或者分辨率改变,里面的控件和字体大小都会自适应改变。

<Window x:Class="WpfTest2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfTest2"
        mc:Ignorable="d"
        Title="MainWindow" WindowState="Maximized">
    <Viewbox>
        <Grid Width="1920" Height="1080">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="400"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="400"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Button Margin="20" Content="左上" FontSize="50" Grid.Column="0" Grid.Row="0"></Button>
            <Button Margin="20" Content="右上" FontSize="50"  Grid.Column="1" Grid.Row="0"></Button>
            <Button Margin="20" Content="下" FontSize="50"   Grid.Column="0"  Grid.ColumnSpan="2" Grid.Row="1" ></Button>
        </Grid>
    </Viewbox>
</Window>

运行后,可以拖拽或者改变电脑分辨率试试效果、

注:如果一开始没有设置最大化,窗口一般不会自适应,需要按比例拖拽才会看到效果

猜你喜欢

转载自blog.csdn.net/qq_42063091/article/details/82781381