如何在 WPF 中的 Dialog Units 中指定单位? [英] How to specify units in Dialog Units in WPF?

查看:31
本文介绍了如何在 WPF 中的 Dialog Units 中指定单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想出了一个简单的以查看这些测量值的来源.
简短版本是:

  • dlu = 对话框单元
  • dlu 基于字体大小(项目随用户字体大小变化)
  • 水平 dlu 与垂直 dlu 不同(dlu 不是方形)

这来自于对话单元的定义:平均字符高 8dlus,宽 4dlus.

格鲁吉亚 14pt:

如果您使用较小的字体(即 8pt Tahoma 和 14pt Georgia),dlus 会变小:

Segoe UI 9pt:

注意:您会注意到分辨率(即 dpi)对讨论没有影响.

解决方案

你可以尝试创建一个 Converter 返回乘以你的 DLU 的值

例如,我创建了一个 MathConverter(代码是 here) 可以让您指定类似

的内容

为了更容易编写和阅读,您可以创建一个继承自 Binding 的类并设置默认绑定属性,这样您所要做的就是

我从未尝试过使用 ConverterConverterParameter 的默认值覆盖绑定,但是为了验证目的我已经覆盖了绑定,所以我相信这是可能的.

我创建的 ValidationBinding 可以像 Text="{local:ValidationBinding MyProperty}" 一样使用并自动设置 ValidatesOnDataError=True, ValidatesOnException=True, UpdateSourceTarget=PropertyChanged 在绑定中.

i'm trying to figure out how to layout a simple dialog in WPF using the proper dialog units (DLUs).

i've come up with a simpler example, that cuts straight to the problem:

You can check the Windows UX Guidelines to see where these measurements come from.
The short version is:

  • dlu = dialog unit
  • dlu is based on the font size (items change with user's font size)
  • a horizontal dlu is different from a vertical dlu (dlu's are not square)

This comes from the definition of a dialog unit: the average character is 8dlus high by 4dlus wide.

Georgia 14pt:

If you use a smaller font (i.e. 8pt Tahoma verses 14pt Georgia), the dlus get smaller:

Segoe UI 9pt:

Note: You'll notice that resolution (i.e. dpi) has no impact on the discussion.

解决方案

You could try creating a Converter which returns the value multiplied by whatever your DLU is

For example, I created a MathConverter (code is here) which would let you specify something like

<Button Height="{Binding Source={x:Static local:Settings.VerticalDLU,
            Converter={StaticResource MathConverter},
            ConverterParameter=@VALUE*14}"

        Width="{Binding Source={x:Static local:Settings.HorizontalDLU,
            Converter={StaticResource MathConverter},
            ConverterParameter=@VALUE*50}" />

To make it easier to write and read, you can create a class that inherits from Binding and setup a default binding properties so all you have to do is

<Button Height="{local:MyVDluBinding Source=14}"
        Width="{local:MyHDluBinding Source=50}" />

I've never tried overwriting a binding with defaults for Converter and ConverterParameter, but I have overwritten a binding for validation purposes so I believe it's possible.

The ValidationBinding I created could be used like Text="{local:ValidationBinding MyProperty}" and automatically set ValidatesOnDataError=True, ValidatesOnException=True, UpdateSourceTarget=PropertyChanged in the bindings.

这篇关于如何在 WPF 中的 Dialog Units 中指定单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆