zoukankan      html  css  js  c++  java
  • WPF 体验数据邦定

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

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

    代码
    <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来邦定数据。

    代码
    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,结果:

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

    代码
    <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,结果:

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

    代码
    <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>
    代码
    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; }
    }

    }

    运行结果:

    应用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; }
        }

  • 相关阅读:
    win10下无法安装loadrunner,提示“管理员已阻止你运行此应用”
    【原创】selenium+python+openpyxl实现登录自动化测试,自动读取excel用例数据,并将数据结果自动写入到excel
    用python+openpyxl从表格中读取测试用例的多条数据,然后将执行结果写入表格中
    用python+openpyxl从表格中读取测试用例的多条数据,然后将执行结果写入表格中,同时生成测试报告
    用Python添加写入数据到已经存在的Excel的xlsx文件
    selenium与webdriver驱动与firefox、 chrome匹配版本
    python3学习之lambda+sort
    小白月赛22 J : 计算 A + B
    小白月赛22 F: 累乘数字
    小白月赛22 E : 方格涂色
  • 原文地址:https://www.cnblogs.com/jimson/p/WPF2.html
Copyright © 2011-2022 走看看