How to specify units in Dialog Units in WPF?

流过昼夜 提交于 2019-11-28 08:19:51

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7147716/how-to-specify-units-in-dialog-units-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!