【问题标题】:xamDataGrid formatting numeric fields to 4 decimal placesxamDataGrid 将数字字段格式化为 4 位小数
【发布时间】:2014-12-26 20:03:08
【问题描述】:

我有一个 WPF 项目,其中包含一个带有 Infragistics xamDataGrid 的表单。此 xamDataGrid 中显示的数据会因调用者而异。

我想要做的是格式化一个数字列 (Rate) 以显示 4 个小数位。我找到了几个站点,其中包含有关如何使用 XAML 执行此操作的说明,但是由于此网格中的数据是动态的,因此我需要在代码隐藏中执行此操作。 (最好是 C#,但我可以用 VB.NET 来管理。)

调用者当前将要在网格中显示的数据以及要隐藏的列和可编辑的列传递给我。所以我可以让它给我一个要格式化的列列表和要使用的格式字符串。

我只需要弄清楚如何告诉 xamDataGrid 以某种方式格式化列或字段。

对于那些想要查看 XAML 的人,这里是数据网格:

<igWPF:XamDataGrid x:Name="GrdMaint"
                   Margin="10"
                   DataSource="{Binding Source={StaticResource cvsDataGrid}}"
                   BorderBrush="Black"
                   BorderThickness="1"
                   HorizontalAlignment="Stretch"
                   VerticalAlignment="Stretch" 
                   IsSynchronizedWithCurrentItem="True"
                   Theme="Office2k7Blue" 
                   GroupByAreaLocation="None" 
                   FieldLayoutInitialized="GrdMaint_OnFieldLayoutInitialized"
                   PreviewMouseDoubleClick="GrdMaint_OnPreviewMouseDoubleClick"
                   RecordUpdated="GrdMaint_RecordUpdated">

    <igWPF:XamDataGrid.FieldSettings>
        <igWPF:FieldSettings AllowRecordFiltering="True" 
                             FilterLabelIconDropDownType="MultiSelectExcelStyle" 
                             Width="Auto" />
    </igWPF:XamDataGrid.FieldSettings>

    <igWPF:XamDataGrid.FieldLayoutSettings>
        <igWPF:FieldLayoutSettings HighlightAlternateRecords="True" 
                                   FilterUIType="LabelIcons" 
                                   AllowDelete ="False" 
                                   AutoGenerateFields="True" />
    </igWPF:XamDataGrid.FieldLayoutSettings>
</igWPF:XamDataGrid>

【问题讨论】:

    标签: wpf infragistics xamdatagrid


    【解决方案1】:

    好的,在与网格斗争并搜索了几天之后,我偶然发现了为我找到答案的神奇搜索字符串。我在 Infragistics 支持论坛上找到了this post(StackOverflow 宠坏了我。这里好多了。)这使我能够提出这个解决方案。在 OnFieldLayoutInitialized 事件中,我可以遍历每个字段并确定是否要格式化该字段。就我而言,我正在检查字段名称是否在字段名称/格式字符串的字典中。如果是这样,我应用格式字符串。

    private void GrdMaint_OnFieldLayoutInitialized(object sender, FieldLayoutInitializedEventArgs e)
    {
        Dictionary<string,string> NumericColumnsToFormat = _Vm.GetNumericColumnFormats(_currentStep);
    
        foreach (var fld in e.FieldLayout.Fields)
        {
            // apply custom numeric format to specific field, if necessary.
            if (NumericColumnsToFormat.Keys.Contains(fld.Name))
            {
                string formatString = NumericColumnsToFormat[fld.Name];
                if (!string.IsNullOrEmpty(formatString))
                {
                    var numberEditorStyle = new Style(typeof(XamNumericEditor));
                    numberEditorStyle.Setters.Add(new Setter(XamNumericEditor.FormatProperty, formatString));
                    fld.Settings.EditorStyle = numberEditorStyle;
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-04
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 2019-01-25
      • 2020-10-12
      • 2011-09-02
      • 1970-01-01
      相关资源
      最近更新 更多