WPF:如何将自定义格式应用于ListView? [英] WPF: How do I apply custom formatting to a ListView?

查看:240
本文介绍了WPF:如何将自定义格式应用于ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ListView非常简单:

My ListView is petty simple:

<ListView ItemsSource="{Binding Path=ActiveCounters}">
    <ListView.View>
        <GridView>
            <GridViewColumn  Header="Name" DisplayMemberBinding="{Binding Path=Name}" />
            <GridViewColumn  Header="Value"  DisplayMemberBinding="{Binding Path=Value}" />
            <GridViewColumn  Header="As Of Date"  DisplayMemberBinding="{Binding Path=AsOfDate}" />
            <GridViewColumn  Header="Duration"  DisplayMemberBinding="{Binding Path=Duration}" />
            <GridViewColumn  Header="Last Modified Date"  DisplayMemberBinding="{Binding Path=Timestamp}" />
        </GridView>
    </ListView.View>
</ListView>

我想做的是:

  • 使用内置格式"D0"设置值"的格式
  • 使用自定义字符串"MMM d hh:mm:ss tt"设置"AsOfDate"和上次修改日期"的格式
  • 使用定义为字符串DurationString(TimeSpan)"的函数格式化持续时间"

推荐答案

对于Value和AsOfDate列,请使用StringFormat属性-WPF 3.5 SP1的新功能. 有关此的更多信息:

For Value and AsOfDate columns use StringFormat attribute - a new feature of WPF 3.5 SP1. More about it here:

http://blogs.msdn.com/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx

http://elegantcode .com/2009/04/07/wpf-stringformat-in-xaml-with-the-stringformat-attribute/

如果要在绑定值上调用自定义函数,请为此实现一个值转换器.

If you want to call a custom function on a bound value, then implement a value converter for that.

http://msdn.microsoft.com/zh-cn/library/system.windows.data.ivalueconverter.aspx

您可以从convert方法调用自定义函数.

You can call your custom function from convert method.

这篇关于WPF:如何将自定义格式应用于ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆