wpf - 将格式设置为 DataGrid -WPF 中显示的数据

标签 wpf mvvm datagrid format

我想在 中为 DataGrid 的列设置格式xml ,列包含双数据。

到目前为止,我在代码中做到了如下:

string format = "{0:F3}";
double nExm= 4.24;
string newExm= string.Format(format, nExm);

数据网格:
<DataGrid x:Name="DG" ItemsSource="{Binding List}" AutoGenerateColumns="False">
     <DataGrid.Columns>
          <DataGridTextColumn Header="{x:Static p:Resources.Exm}" Binding="{Binding Exm}"></DataGridTextColumn>
     </DataGrid.Columns>
 </DataGrid>

newExm 它包含在包含绑定(bind)到 DataGrid 的列表的类 X 中

X类:

私有(private)字符串 exm;
    public string Exm
    {
        get { return exm; }
        set
        {
            exm= value;
            NotifyPropertyChanged("Exm");
        }
    }

有没有办法通过 XAML 设置格式? (我想将变量翻倍并在列上设置格式)

最佳答案

您可以使用 StringFormat在您的 Binding 上格式化它:

  • http://blogs.msdn.com/b/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx
  • Binding="{Binding YourDoublePropertyHere, StringFormat=F3}"
    如果 StringFormat不能满足你所​​有的格式化需求(在这种情况下它应该足够了),或者你有更复杂的转换逻辑然后你可以写一个 Converter按摩数据。
  • http://wpftutorial.net/ValueConverters.html
  • 关于wpf - 将格式设置为 DataGrid -WPF 中显示的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14728822/

    相关文章:

    c# - 有没有办法检查 WPF 当前是否在设计模式下执行?

    MVVM:负责加载相关数据,在哪一层?

    datagrid - VB6 更好的网格支持行和列跨越?

    WPF .NET 4 DataGrid 列性能

    c# - 组合不使用 MVVM 和 XAML 更新

    datagrid - Material-UI DataGrid : How do I add a tooltip to each row cell?

    c# - WPF 绑定(bind) - 选中 X 时绑定(bind)到 A,否则绑定(bind)到 B

    c# - 如何在 WPF 中调整多边形的大小?

    c# - 系列数量未知的 XamDataChart

    c# - MVVM 基础 View 模型类