按钮不应缩放Dpi的依存关系

时间:2019-04-03 09:33:19

标签: wpf

我用不同单位的按钮做了一些WPF测试应用程序。 现在,我在96dpi的屏幕和226dpi的屏幕上运行它。

我期望的是,不带单位的按钮和带px单位的按钮会比226dpi显示屏上的其他按钮小-但它们保持相同的大小。

如何强制“像素”按钮使用真实像素?独立于dpi分辨率或用户将缩放比例设置为200%。

在我的真实应用程序中,我希望菜单和树等是dpi感知的,但是在中间,我在用户控件中有一些图形,我希望该用户控件使用真实像素。

这是演示代码:

<Window x:Class="WpfApp2.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"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Grid.Column="0" Content="96 x 48" Width="96" Height="48" />
    <Button Grid.Row="1" Grid.Column="0" Content="96px x 48px" Width="96px" Height="48px" />
    <Button Grid.Row="2" Grid.Column="0" Content="1.0in x 0.5in" Width="1.0in" Height="0.5in" />
    <Button Grid.Row="3" Grid.Column="0" Content="2.54cm x 1.27cm" Width="2.54cm" Height="1.27cm" />
  </Grid>
</Window>  

1 个答案:

答案 0 :(得分:0)

我发现了这个小的Decorator-Class,并使用它解决了我的问题。

Decorator

但是,如果有人患有例如三个监视器,并在每个监视器上使用不同的缩放比例。装饰器不使用运行应用程序的监视器的设置。