惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - xqiwei

WCF RIA Services DomainService life-cycle and adding Transactions C#多线程 使用委托更新UI实例(WP7开发 其他线程中更新UI)(转载) Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) looping(and modifying) a collection - xqiwei LineBreak in a tooltip in xaml - xqiwei GetObjectbyKey in E.F. vs. Querying for a single entity Use GetObjectByKey() for better performance Visual Studio集成开发环境无法启动调试 C#中关于String.Equals(object,object)和(object==object )的比较 - xqiwei - 博客园 ASP.NET页面生命周期和asp.net应用程序生命周期 Windows Presentation Foundation Tools and Controls ArcEngine开发之Command控件使用篇 Resharper进阶 C# 中的委托和事件 WPF系列文章 新技术文章 - xqiwei flash与javascript、asp.net(数据库)的交互 使用vs.net ajax实现幻灯片的效果 职业规划
IValueConverter 接口
xqiwei · 2010-01-27 · via 博客园 - xqiwei

 

提供一种将自定义逻辑应用于绑定的方式。

命名空间:  System.Windows.Data
程序集:  PresentationFramework(在 PresentationFramework.dll 中)

 语法

Public Interface IValueConverter
Dim instance As IValueConverter
public interface IValueConverter
public interface class IValueConverter
public interface IValueConverter
public interface IValueConverter
可以直接在 XAML 中使用接口,请参见实现该接口的类型。

 备注

 示例

本示例演示如何将转换应用于绑定中使用的数据。

要在绑定期间转换数据,必须创建一个实现 IValueConverter 接口的类,其中包括 ConvertConvertBack 方法。

下面的示例演示一个日期转换器的实现,此日期转换器转换传入的日期值,使其只显示年月日。实现 IValueConverter 接口时,最好用 ValueConversionAttribute 属性来修饰此实现,以便向开发工具指示转换所涉及的数据类型,如下面的示例所示:

C#

[ValueConversion(typeof(DateTime), typeof(String))]
public class DateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime date = (DateTime)value;
        return date.ToShortDateString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string strValue = value.ToString();
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime))
        {
            return resultDateTime;
        }
        return value;
    }
}

一旦创建了转换器,即可将其作为一项资源添加到可扩展应用程序标记语言 (XAML) 文件中。在下面的示例中,src 映射到在其中定义 DateConverter 的命名空间。

<src:DateConverter x:Key="dateConverter"/>

最后,通过以下语法在绑定中使用转换器。在下面的示例中,TextBlock 的文本内容绑定到 StartDate,后者是外部数据源的一个属性。

<TextBlock Grid.Row="2" Grid.Column="0" Margin="0,0,8,0"
           Name="startDateTitle"
           Style="{StaticResource smallTitleStyle}">Start Date:</TextBlock>
<TextBlock Name="StartDateDTKey" Grid.Row="2" Grid.Column="1" 
    Text="{Binding Path=StartDate, Converter={StaticResource dateConverter}}" 
    Style="{StaticResource textStyleTextBlock}"/>