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

时间:2008-12-27 17:37:23

标签: wpf windows xaml dpi

我正在尝试弄清楚如何使用正确的对话框单元(DLU)在WPF中布局简单对话框。


  

什么是对话单元?

     

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

     

enter image description here

     

这意味着对话框单元:

     
      
  • 使用所选字体进行更改
  •   
  • 使用所选的DPI设置进行了更改
  •   
  • 不是正方形
  •   

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

alt textImage Link

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

到目前为止,我唯一能够提出的XAML(使用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的大小不同。


另见

凹凸: 6/20/2011

5 个答案:

答案 0 :(得分:7)

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

请注意,我在标记中加倍了DLU单元 - 因此保持相同的方面。按键高度为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一样,这可以通过多种方式完成 - 这只是一种解决方案。

希望这有帮助!

答案 1 :(得分:2)

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

Final Result

首先,我必须找到一种方法来获取给定字体的给定字母的宽度和高度(在我的情况下,Segoe UI为10px)...为此,我使用了这个SO答案:{{3}我创建了一个静态类来保存生成的双精度数:

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>

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

Converting from DLUs to relative pixels and back

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

答案 4 :(得分: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>
相关问题