WPF 体验---数据邦定

本文介绍了如何在WPF中使用ListBox进行数据绑定,并展示了如何设置ItemSource属性来展示Person对象集合。此外,还演示了如何使用DataTemplate来自定义每个项的显示方式,包括处理空值的情况。

WPF主要做UI,所以数据邦定当然少不了。本节以ListBox为例,介绍数据邦定(和WinForm还是很有区别的哦)。

先建立一个WPFApplication Project. 在Window xaml里添加一个ListBox控件。

 

ContractedBlock.gifExpandedBlockStart.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>
</Grid>
</Window>

引用一个Loaded事件来完成我们的邦定。大家知道,ListBox是一个ItemControl,所以我们通过设置ItemSource来邦定数据。

 

ContractedBlock.gifExpandedBlockStart.gif代码
public partial class Test : Window
{
public Test()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
List
<Person> persons = new List<Person>{
new Person{FirstName="jimson",LastName="Ma"},
new Person{FirstName="Lingxia",LastName="Wang"}
};

this.lbxPersons.ItemsSource = persons;
}
}

public class Person{
public string LastName { set; get; }
public string FirstName { set; get; }
}

运行F5,结果:

 

2010052323443477.png

显然这样是不成的。改进xaml:

 

ContractedBlock.gifExpandedBlockStart.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>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}" Width="50" />
<TextBlock Text="{Binding LastName}" Width="50" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>

运行F5,结果:

 

2010052323560335.png

这下正确了。但是有个问题,空值怎么办?看看这个:

 

ContractedBlock.gifExpandedBlockStart.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>
<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>
ContractedBlock.gifExpandedBlockStart.gif代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfProject
{
/// <summary>
/// Interaction logic for Test.xaml
/// </summary>
public partial class Test : Window
{
public Test()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
List
<Person> persons = new List<Person>{
new Person{FirstName="jimson",LastName="Ma", Age=23},
new Person{FirstName="Lingxia",LastName="Wang"}
};

this.lbxPersons.ItemsSource = persons;
}
}

public class Person{
public string LastName { set; get; }
public string FirstName { set; get; }
public int? Age { set; get; }
}

}

运行结果:

 

2010052400062177.png

应用TargetNullValue,很好的处理了控制信息。

 

 

 

public partial class Test : Window
    {
        public Test()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<Person> persons = new List<Person>{
                new Person{FirstName="jimson",LastName="Ma"},
                new Person{FirstName="Lingxia",LastName="Wang"}
            };

            this.lbxPersons.ItemsSource = persons;
        }
    }

    public class Person{
        public string LastName { set; get; }
        public string FirstName { set; get; }
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值