WPF 体验---数据邦定(二)

本文介绍了WPF中使用MultiBinding和StringFormat进行数据绑定的方法,通过实例展示了如何更优雅地展示绑定数据,并利用FallbackValue处理不存在的绑定路径。

上一节,提到了控制空值的TargetNullValue。本节介绍下另外两个跟绑定有关的关键字:StringFormat,FallbackValue。

 

StringFormat,顾名思义就是格式化字符串,在数据绑定的过程中,这个当然很重要了,谁不爱美阿。先看看上节的例子:

 


  
< Window x:Class = " WpfProject.Test "
xmlns
= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x
= " http://schemas.microsoft.com/winfx/2006/xaml "
Title
= " Test " Height = " 300 " Width = " 300 " Loaded = " Window_Loaded " >
< Grid >
< ListBox Name = " lbxPersons " >
< ListBox.ItemTemplate >
< DataTemplate >
< StackPanel Orientation = " Horizontal " >
< TextBlock Text = " {Binding FirstName} " Width = " 50 " />
< TextBlock Text = " {Binding LastName} " Width = " 50 " />
< TextBlock Text = " {Binding Age, TargetNullValue='Age Unknown'} " />
</ StackPanel >
</ DataTemplate >
</ ListBox.ItemTemplate >
</ ListBox >
</ Grid >
</ Window >

为了很好的显示我们所需要的数据,用了三个TextBlock,显得很啰嗦有很吃力,那么有没有更好的方法呢,用MultiBinding和StringFormat。

 

 

ContractedBlock.gif ExpandedBlockStart.gif 代码

   
< Window x:Class = " WpfProject.Test "
xmlns
= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x
= " http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:d
= " clr-namespace:System.Windows.Data;assembly=PresentationFramework "
Title
= " Test " Height = " 300 " Width = " 300 " Loaded = " Window_Loaded " >
< Grid >
< ListBox Name = " lbxPersons " >
< ListBox.ItemTemplate >
< DataTemplate >
< TextBlock >
< TextBlock.Text >
< MultiBinding StringFormat = " {}{0}-{1},{2} " >
< Binding Path = " FirstName " />
< Binding Path = " LastName " />
< Binding Path = " Age " TargetNullValue = ' Age Unknown ' />
</ MultiBinding >
</ TextBlock.Text >
</ TextBlock >
</ DataTemplate >
</ ListBox.ItemTemplate >
</ ListBox >
</ Grid >
</ Window >

2010052422561132.png

 

 

FallbackValue设置邦定不存在时显示的信息。还是上面的例子,Person类里不存在Birthday属性,我们在绑定里添加,让它显示不存在。

ContractedBlock.gif ExpandedBlockStart.gif 代码

   
< Window x:Class = " WpfProject.Test "
xmlns
= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x
= " http://schemas.microsoft.com/winfx/2006/xaml "
Title
= " Test " Height = " 300 " Width = " 300 " Loaded = " Window_Loaded " >
< Grid >
< ListBox Name = " lbxPersons " >
< ListBox.ItemTemplate >
< DataTemplate >
< TextBlock >
< TextBlock.Text >
< MultiBinding StringFormat = " {}{0}-{1},{3},{2} " >
< Binding Path = " FirstName " />
< Binding Path = " LastName " />
< Binding Path = " Age " TargetNullValue = ' Age Unknown ' />
< Binding Path = " Birthday " FallbackValue = ' Birthday Unknown ' />
</ MultiBinding >
</ TextBlock.Text >
</ TextBlock >
</ DataTemplate >
</ ListBox.ItemTemplate >
</ ListBox >
</ Grid >
</ Window >
运行:

2010052423021174.png

OK。

 

<Window x:Class="WpfProject.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         
    Title="Test" Height="300" Width="300" Loaded="Window_Loaded">
    <Grid>
        <ListBox Name="lbxPersons">  
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock>
                        <TextBlock.Text>
                            <MultiBinding  StringFormat="{}{0}-{1},{3},{2}">
                                <Binding Path="FirstName"/>
                                 <Binding Path="LastName"/>
                                 <Binding Path="Age" TargetNullValue='Age Unknown'/>   
                                <Binding Path="Birthday" FallbackValue='Birthday Unknown'/>  
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

转载于:https://www.cnblogs.com/jimson/archive/2010/05/24/WPFDataBing2.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值