【问题标题】:How to specify units in Dialog Units in WPF?如何在 WPF 中的对话框单位中指定单位?
【发布时间】:2011-08-22 12:49:34
【问题描述】:

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

我想出了一个简单的r 示例,可以直接解决问题:

您可以查看Windows UX Guidelines 了解这些测量值的来源。
简短的版本是:

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

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

格鲁吉亚 14pt:

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

Segoe UI 9pt:

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

【问题讨论】:

标签: wpf user-interface user-experience


【解决方案1】:

您可以尝试创建一个Converter,它返回乘以您的 DLU 的值

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

<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}" />

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

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

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

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

【讨论】:

  • 为什么伊恩不接受这个答案?看起来很合理。我打算建议子类化并添加新的 DependencyProperties。你的解决方案看起来更干净。此外,附加属性也可能有效。
猜你喜欢
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
  • 2016-08-27
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多