【问题标题】:Extended WPF Toolkit PropertyGrid Number format扩展的 WPF 工具包 PropertyGrid 数字格式
【发布时间】:2012-11-15 14:56:20
【问题描述】:

我正在使用扩展 WPF 工具包中的 PropertyGird。我几乎可以做任何我需要的事情,但我无法格式化数字。

我有一个double 属性,我希望它只显示两个十进制数字(其字符串格式应该是"F2")。我尝试过添加[DisplayFormat(DataFormatString = "{F2}")] 属性,但它似乎没有任何效果(我仍然有我的 10 位数字)。

我做错了吗?我真的需要为double 类型创建一个CustomEditor,它会像这样格式化所有我的double 属性吗?

感谢任何帮助!

编辑:使用网格的 AutoGenerateProperties 选项自动绑定属性。我没有明确的绑定。如果可能的话,我想保持这种方式,但这不是强制性的。

【问题讨论】:

  • 您能展示一下您是如何绑定该属性的吗?

标签: wpf xaml propertygrid


【解决方案1】:

我终于找到了在 PropertyGrid.EditorDefinitions 中使用 DataTemplate 的方法。在下面的示例中,每个 Double 类型的属性都会获得一个格式为“F2”的“DoubleUpDown”编辑器。

xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"

<xctk:PropertyGrid ...>
    <xctk:PropertyGrid.EditorDefinitions>
        <xctk:EditorTemplateDefinition>

            <xctk:EditorTemplateDefinition.TargetProperties>
                <xctk:TargetPropertyType Type="{x:Type System:Double}" />
            </xctk:EditorTemplateDefinition.TargetProperties>

            <xctk:EditorTemplateDefinition.EditingTemplate> 
                <DataTemplate>
                    <xctk:DoubleUpDown FormatString="F2" 
                                       Value="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </xctk:EditorTemplateDefinition.EditingTemplate>

        </xctk:EditorTemplateDefinition>
    </xctk:PropertyGrid.EditorDefinitions> 
</xctk:PropertyGrid>

通过在EditorTemplateDefinition.TargetProperties中命名特定属性,只有这些属性会受到后面的DataTemplate的影响。

<xctk:EditorTemplateDefinition.TargetProperties>
    <System:String>Volume</System:String>
    <System:String>Weight</System:String>
</xctk:EditorTemplateDefinition.TargetProperties>

【讨论】:

    【解决方案2】:

    我只能找到一种方法(非常肮脏):

    void PropertyGrid_SelectedObjectChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        foreach (var p in pg.Properties)
        {
            if (p.PropertyType == typeof(double)) // or filter by p.Name
                p.Value = string.Format("{0:F2}", p.Value);
        }
    }
    

    【讨论】:

    • 你说得对,它很脏 :D。无论如何,最后我们决定“所有doubles 的两位数都可以”,所以我将为所有double 属性放置一个自定义编辑器。我仍然对大多数属性都在工作感到困惑,但DisplayFormat 不是。无论如何,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2015-05-19
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多