WPF:如何在对话单元中指定单位?

问题描述 投票:25回答:5

我试图弄清楚如何使用适当的对话单元(DLU)在WPF中布局一个简单的对话框。


什么是对话单元?

对话框是基于用户首选字体大小的度量单位。定义一个对话框单元,使得平均字符宽4个对话框单位,高8个对话框单位:

这意味着对话单元:

  • 用所选字体更改
  • 已选择DPI设置更改
  • 不正方形

我花了大约两个小时从Windows Vista中使用各种dlu测量标注这个示例对话框。有人可以给出生成此对话框的相应XAML标记吗?

alt textImage Link

现在我承认我对WPF XAML几乎一无所知。每次我开始,我都会受到阻碍,因为我无法弄清楚如何进行任何控制。似乎WPF中的所有内容都必须包含在某种面板中。有StackPanels,FlowPanels,DockPanel,Grid等。如果你没有这些,那么它将无法编译。

到目前为止,我唯一能够提出的XAML(uing XAMLPad):

<DockPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Image Width="23" />
    <Label>Are you sure you want to move this file to the Recycle Bin?</Label>
    <Image Width="60" />
    <Label>117__6.jpg</Label>
    <Label>Type: ACDSee JPG Image</Label>
    <Label>Rating: Unrated</Label>
    <Label>Dimensions: 1072 × 712</Label>
    <Button Content="Yes" Width="50" Height="14"/>  
    <Button Content="Cancel" Width="50" Height="14"/>  
</DockPanel>

这是一个华而不实的怪物。没有任何控件放置或尺寸正确。我无法弄清楚如何在窗口中定位控件,也无法正确调整它们的大小。

有人可以将该屏幕截图转换为XAML吗?

注意:您不能测量屏幕截图。指定了所有对话单位(dlu)的宽度和高度。

注意:1个水平DLU!= 1个垂直DLU。水平和垂直DLU的大小不同。


See also

凹凸:2011年6月20日

wpf windows xaml dpi
5个回答
7
投票

以下XAML将为您提供所需的效果。

请注意,我将标记中的FLU单位加倍 - 从而保持相同的方面。按钮高度为14个单位,看起来很有趣。您可能需要修补市场上的数字。

此外,我开始将一些“Vista布局”删除为单独的样式。您可以继续沿着这条路走下去,这样您就拥有了一套符合Vista准则的可重复使用的样式。我很确定其他人做过类似的事情。

此外,我对对话的大小采取了一些自由。你提到你想要210x96单位 - 你需要设置这个数量,加上窗口铬。

无论如何,关于内容:

  <Window x:Class="VistaLayout.Dialog"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Delete File" 
      ResizeMode="NoResize"
      Height="212" Width="430">
    <Window.Resources>
      <Style x:Key="FooterButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Width" Value="100" />
        <Setter Property="Height" Value="28" />
        <Setter Property="Margin" Value="8,0,0,0" />
      </Style>
      <Style x:Key="FooterPanelStyle" TargetType="{x:Type UniformGrid}">
        <Style.Resources>
          <Style TargetType="{x:Type Button}" BasedOn="{StaticResource FooterButtonStyle}" />
        </Style.Resources>
        <Setter Property="Rows" Value="1" />
        <Setter Property="HorizontalAlignment" Value="Right" />
      </Style>
    </Window.Resources>
    <DockPanel Margin="14">
      <!-- Footer -->
      <UniformGrid DockPanel.Dock="Bottom" 
                       Style="{StaticResource FooterPanelStyle}">
        <Button>_Yes</Button>
        <Button>_No</Button>
      </UniformGrid>

      <!-- Main Content -->
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition Width="8" />
          <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Image Width="64" />

        <StackPanel Grid.Column="2">
          <TextBlock Margin="0,6,0,14">Are you sure you want to move this file to the Recycle Bin?</TextBlock>

          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="Auto" />
              <ColumnDefinition Width="14" />
              <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Image Width="60" />

            <StackPanel Grid.Column="2">
              <TextBlock>117__6.jpg</TextBlock>
              <TextBlock>Type: ACDSee JPG Image</TextBlock>
              <TextBlock>Rating: Unrated</TextBlock>
              <TextBlock>Dimensions: 1072 × 712</TextBlock>
            </StackPanel>

          </Grid>

        </StackPanel>

      </Grid>

    </DockPanel>
  </Window>

与大多数XAML一样,这可以通过多种方式完成 - 这只是一种解决方案。

希望这可以帮助!


2
投票

我知道这已经很老了,但我想我会尝试做OP所要求的。因此,这是我的尝试。顺便说一句,在我继续之前,我应该指出,由于某种原因,OPs测量在使用DLU时并没有完全解决,但我认为我已经相当接近了。另外请记住,当涉及到这些东西时,我仍然是一个亲戚...所以如果我做错了什么或亵渎神明......道歉。

首先,我必须找到一种方法来获得给定字体的给定字母的宽度和高度(在我的情况下,Segoe UI为10px)...为此,我使用了这个SO答案:how-to-calculate-wpf-textblock-width-for-its-known-font-size-and-characters,我做了一个静态用于保存结果双打的类:

public static class Fonts
{
    public static double HorizontalDluMultiplier;
    public static double VerticalDluMultiplier;

    static Fonts()
    {
        var formattedText = new FormattedText(
            "A",
            CultureInfo.CurrentUICulture,
            FlowDirection.LeftToRight,
            new Typeface("Segoe UI"),
            12.0,
            Brushes.Black);
        Fonts.HorizontalDluMultiplier = formattedText.Width / 4;
        Fonts.VerticalDluMultiplier = formattedText.Height / 8;
    }
}

一旦我有了指标,我必须创建一个WPF转换器,它接受一个给定的ConverterParameter(在这种情况下是DLU中的数字)并吐出一个像素的两倍。这是我用过的转换器......

public class HorizontalDluToPixelConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (Double.Parse((parameter as string))) * Fonts.HorizontalDluMultiplier;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我认为不用说我有一个单独的垂直版本的转换器。

一旦完成,我只需要在XAML中布置窗口,并在设置高度和宽度时使用转换器。我用一个网格来布局整个窗口。但是为了设置列宽和行高,我使用了转换器,如下所示:

<Window.Resources>
    <converters:HorizontalDluToPixelConverter x:Key="HorizontalConverter" />
    <converters:VerticalDluToPixelConverter x:Key="VerticalConverter" />
</Window.Resources>

<Grid.RowDefinitions>
    <RowDefinition Height="{Binding Converter={StaticResource VerticalConverter}, ConverterParameter=7}" />
    etc...
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="{Binding Converter={StaticResource HorizontalConverter}, ConverterParameter=7}" />
    etc... etc...
</Grid.ColumnDefinitions>

希望这也有助于未来的人(如果它实际上是有帮助的嘿)


1
投票

查看Grid control - 它支持相对大小。


1
投票

这是我在MSDN上发现的关于Layout Metrics的更详细的链接。 WPF DIU定义为1/96英寸,DLU到像素转换是字体相关的,如下表所示。

因此,使用此信息以及系统DPI设置并根据您所定位的字体,您可以计算出垂直或水平DLU单位中给定测量的DUI数量。我还没有看到任何基于javascript的计算器,但是在任何编程语言中创建类似的工具都会非常简单。


0
投票

Canvas布局元素允许基于坐标的布局,类似于您习惯使用的布局,如果您有Canvas,您甚至可以在可视化编辑器中获得一些指导。例如:

<Window xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:d='http://schemas.microsoft.com/expression/blend/2008' mc:Ignorable='d' Title='Spin-Echo Image Processing' Width='673' x:Class='ImageR2.CLASPmap' Height='961' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
    <Canvas Name='canvas1'>
        <TextBlock Name='TEXT_Program' Canvas.Top='27' Width='133' Height='21' Canvas.Left='875'>CLASPmap:</TextBlock>
        <TextBlock Name='TEXT_Heading' Canvas.Top='27' Width='368' Height='27' Canvas.Left='1008'>Transverse Relaxation Rate Mapping</TextBlock>
        <TextBlock Name='TEXT_XYCoordinates' Canvas.Top='251' Width='139' Height='21' Canvas.Left='869'>X &amp; Y Coordinates</TextBlock>
© www.soinside.com 2019 - 2024. All rights reserved.