您好,
会员登录 快速注册
退出 ( 条未读消息)
关于本站 意见反馈 首页

公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!
全部文章分类
  • 人工智能 >

  • 编程语言 >

  • WPF系列 >

  • ASP.NET系列 >

  • Linux >

  • 数据库 >

  • 嵌入式 >

  • WEB技术 >

  • PLC系列 >

  • 微服务与框架 >

  • 小宅DIY >

  • 学习资料 >

OpenCv基础 ANN车牌识别 yolov5车牌识别 指针式仪表识别 ROS系列 YOLO Halcon Detectron2 昇腾AI ChatGPT在线体验 英伟达JETSON ChatGLM ChatTTS FunASR 地平线 ByteTrack 魔搭社区 LangChain
C C# C++ Python Java Go
WPF
ASP.NET小功能 GPS定位系统-MVC GPS定位系统-VUE ASP.NET WebRTC
Linux Linux内核 Shell MakeFile
MySql SqlServer Oracle
STM8 STM32 51单片机
VUE入门 HTML JavaScript CSS layui镜像网站 ElementUi中文官网 element-plus 图标
三菱 欧姆龙 西门子 施耐德 松下 台达
IOTSharp IOTGateway ABP FRAMEWORK Docker
亚克力音响 编程仙途:智驭万法
面试题与技巧 Python入门技能树 微软C#教程
首页 编程之美 工具下载 全国就业 流量地图 文心一言
WPF
内容介绍 1、新建WPF工程 2、Application介绍 3、Dispatcher介绍 4、Window 介绍 5、主要布局属性介绍 6、Grid网格布局 7、UniformGrid布局 8、DockPanel与ViewBox布局 9、Border与ViewBox布局 10、依赖属性(一) 11、依赖属性(二) 12、依赖属性(三) 13、依赖属性(四) 14、WPF中的数据绑定(一) 15、WPF中的数据绑定(二) 16、WPF中的数据绑定(三) 17、WPF中的数据绑定(四) 18、ListView示例(一) 19、ListView示例(二) 20、DataGrid示例(一) 21、DataGrid示例(二) 22、DataGrid示例(三) 23、引用FontAwesome矢量图 24、ListBox日志效果 25、Polygon绘制多边形 26、Ellipse绘制实心圆 27、数据模板DataTemplate 基于WPF的exe远程升级程序
15、WPF中的数据绑定(二)
17、WPF中的数据绑定(四)
激萌の小宅 博客园 WPF

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 1

    累计评价

WPF中的数据绑定(三) - (十六)


四、 XML数据绑定

        这次我们来学习新的绑定知识,XML数据绑定。XmlDataProvider 用来绑定 XML 数据,该XML数据可以是嵌入.Xmal文件的 XmlDataProvider 标记中,也可以是外部位置引用的文件中。

        当然嵌入式 XML 内容必须置于 XmlDataProvider 内部的 

        XmlDataProvider 也可以指向 XML 内容的外部源。例如,项目中一个 colors.xml 文件,文件的内容就是一个颜色列表。需要在  

        中添加一个 XmlDataProvider 资源,并将其的Source设置为 XML 文件名即可。 代码与XML文件如下。

<!--Resources一定要放在ListBox前面-->
<StackPanel.Resources>
    <XmlDataProvider x:Key="MyColors" Source="Colors.xml"  XPath="colors"></XmlDataProvider>
</StackPanel.Resources>


        资源绑定语法与控件绑定语法略有不同。绑定到控件时,可以设置绑定的 ElementName 和 Path 属性。但是绑定到资源时,需要设置 Source 属性,由于我们是绑定到 XmlDataProvider,所以还要设置绑定的 XPath 属性。例如,下列代码可将 ListBox 的项绑定 MyColors资源。将 Source 属性设置为资源,并将其指定为名为 MyColors 的 StaticResource。XPath 属性指示项会绑定到 XML 数据源中 

<TextBlock Width="248" Height="24" Text="XML数据绑定:" TextWrapping="Wrap"/>
   <ListBox x:Name="listXmlColor" Width="248" Height="56" IsSynchronizedWithCurrentItem="True"
      ItemsSource="{Binding Source={StaticResource MyColors},XPath=color/@name}">
   </ListBox>
   <TextBlock Width="248" Height="24" Text="选中的颜色:" />
<TextBlock Width="248" Height="24" Text="{Binding ElementName=listXmlColor,  Path=SelectedValue, Mode=OneWay}"></TextBlock>


Colors.xml代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<colors >
  <color name="Pink"/>
  <color name="Red"/>
  <color name="Purple"/>
  <color name="Cyan"/>
  <color name="Gray"/>
  <color name="Turquoise"/>
</colors>


最终代码如下:

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="200" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="150"/>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0">
            <!--Resources一定要放在ListBox前面-->
            <StackPanel.Resources>
                <XmlDataProvider x:Key="MyColors" Source="Colors.xml"  XPath="colors"></XmlDataProvider>
            </StackPanel.Resources>
            
            <TextBlock Width="248" Height="24" Text="XML数据绑定:" TextWrapping="Wrap"/>
            <ListBox x:Name="listXmlColor" Width="248" Height="56" IsSynchronizedWithCurrentItem="True"
                     ItemsSource="{Binding Source={StaticResource MyColors},XPath=color/@name}">
            </ListBox>
            <TextBlock Width="248" Height="24" Text="选中的颜色:" />
            <TextBlock Width="248" Height="24" Text="{Binding ElementName=listXmlColor,  Path=SelectedValue, Mode=OneWay}"></TextBlock>
        </StackPanel>
    </Grid>
</Window>


结果如下图:


五、对象绑定和数据模板

       虽然 XmlDataProvider 对 XML 非常有用,但是当您想绑定到对象或对象列表时,可以创建 ObjectDataProvider 作为资源。ObjectDataProvider 的 ObjectType 指定将提供数据绑定源的对象,而 MethodName 则指示为获得数据而需调用的方法。例如,假设我有一个名为 StudentService 的类,该类使用一种名为 GetStudentList的方法来返回列表 

<ObjectDataProvider x:Key="students" ObjectType="{x:Type local:StudentService}" MethodName="GetStudentList">
</ObjectDataProvider>


       ObjectDataProvider 还可以使用许多其他属性。ConstructionParameters 属性允许您将参数传递给要调用的类的构造函数。此外,可以使用 MethodParameters 属性来指定参数,同时还可以使用 ObjectInstance 属性来指定现有的对象实例作为源。

       如果希望异步检索数据,可以将 ObjectDataProvider 的 IsAsynchronous 属性设为 true。这样,用户将可以在等待数据填充绑定到 ObjectDataProvider 的源的目标控件时与屏幕进行交互。

       在添加 ObjectDataProvider 时,必须限定数据源类的命名空间。在本例中,我必须将 xmlns 属性添加到 

xmlns:local="clr-namespace:WpfApp.Services"


       既然数据源已通过 ObjectDataProvider 定义,接下来就是如何将数据显示在 ListBox 控件。我要把姓名、年龄、出生日期、国籍在每个 ListBoxItem 中一行显示。姓名用粗体,年龄、出生日期、国籍使用默认字体显示。这在 XAML 中,通过使用数据模板(DataTemplate)很容易实现的,DataTemplate 允许您定义自己的显示样式。

       如下代码,在XAML代码中我将 DataTemplate 定义成如何显示Student信息的布局样式。我通过设置 DataTemplate 的 DataType 属性为students,告诉 DataTemplate 将要引用 Student类型。

       我将对象数据students绑定到 ListBox 的 ItemsSource 属性,这样就把将数据绑定到 ListBox了,但是我没有指定如何显示绑定的数据,显示样式是通过将 ItemTemplate 属性设置为 studentLayout资源(即 DataTemplate 的键名),就可以根据我之前在模板中设计的显示样式显示数据了。最终代码如下:

MainWindow.xaml 代码如下:

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp.Services"
        Title="MainWindow" Height="200" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="150"/>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0">
            <StackPanel.Resources>
                <ObjectDataProvider x:Key="students" ObjectType="{x:Type local:StudentService}" MethodName="GetStudentList"></ObjectDataProvider>
                <DataTemplate x:Key="studentLayout" DataType="students">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="Blue"/>
                        <TextBlock Text=", "></TextBlock>
                        <TextBlock Text="{Binding Path=Age}"></TextBlock>
                        <TextBlock Text=", "></TextBlock>
                        <TextBlock Text="{Binding Path=Birthday}"></TextBlock>
                        <TextBlock Text=", "></TextBlock>
                        <TextBlock Text="{Binding Path=Country}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </StackPanel.Resources>
            <TextBlock Width="248" Height="24" Text="对象数据绑定:" TextWrapping="Wrap"/>
            <ListBox x:Name="listObjectBind" Width="450" Height="100" IsSynchronizedWithCurrentItem="True"
                     ItemsSource="{Binding Source={StaticResource students}}"
                     ItemTemplate="{DynamicResource studentLayout}">
            </ListBox>
        </StackPanel>
    </Grid>
</Window>


StudentService.cs 代码如下:

using System.Collections.Generic;

namespace WpfApp.Services
{
    public class Student
    {
        public string Age { get; set; }
        public string Name { get; set; }
        public string Birthday { get; set; }
        public string Country { get; set; }
    }

    public class StudentService
    {
        public List<Student> GetStudentList()
        {
            Student liang = new Student();
            liang.Age = "18";
            liang.Name = "梁丘";
            liang.Birthday = "1990-02-03";
            liang.Country = "中国";
            Student zuo = new Student();
            zuo.Age = "22";
            zuo.Name = "左丘";
            zuo.Birthday = "1992-02-03";
            zuo.Country = "中国";
            Student diwu = new Student();
            diwu.Age = "32";
            diwu.Name = "第五言";
            diwu.Birthday = "1982-11-03";
            diwu.Country = "中国";
            Student yang = new Student();
            yang.Age = "12";
            yang.Name = "羊舌微";
            yang.Birthday = "2002-11-13";
            yang.Country = "中国";
            List<Student> personList = new List<Student>();
            personList.Add(liang);
            personList.Add(zuo);
            personList.Add(diwu);
            personList.Add(yang);
            return personList;
        }
    }
}


效果如下:

15、WPF中的数据绑定(二)
17、WPF中的数据绑定(四)

友情链接: CSDN激萌の小宅 95知识库 自考题库 罗分明个人网络博客 精益编程leanboot

小宅博客  www.bilibili996.com All Rights Reserved. 备案号: 闽ICP备2024034575号

网站经营许可证  福建省福州市 Copyright©2021-2025 版权所有

小宅博客
首页 智能家居 地图定位
公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 1

    累计评价

WPF中的数据绑定(三) - (十六)


四、 XML数据绑定

        这次我们来学习新的绑定知识,XML数据绑定。XmlDataProvider 用来绑定 XML 数据,该XML数据可以是嵌入.Xmal文件的 XmlDataProvider 标记中,也可以是外部位置引用的文件中。

        当然嵌入式 XML 内容必须置于 XmlDataProvider 内部的 

        XmlDataProvider 也可以指向 XML 内容的外部源。例如,项目中一个 colors.xml 文件,文件的内容就是一个颜色列表。需要在  

        中添加一个 XmlDataProvider 资源,并将其的Source设置为 XML 文件名即可。 代码与XML文件如下。

<!--Resources一定要放在ListBox前面-->
<StackPanel.Resources>
    <XmlDataProvider x:Key="MyColors" Source="Colors.xml"  XPath="colors"></XmlDataProvider>
</StackPanel.Resources>


        资源绑定语法与控件绑定语法略有不同。绑定到控件时,可以设置绑定的 ElementName 和 Path 属性。但是绑定到资源时,需要设置 Source 属性,由于我们是绑定到 XmlDataProvider,所以还要设置绑定的 XPath 属性。例如,下列代码可将 ListBox 的项绑定 MyColors资源。将 Source 属性设置为资源,并将其指定为名为 MyColors 的 StaticResource。XPath 属性指示项会绑定到 XML 数据源中 

<TextBlock Width="248" Height="24" Text="XML数据绑定:" TextWrapping="Wrap"/>
   <ListBox x:Name="listXmlColor" Width="248" Height="56" IsSynchronizedWithCurrentItem="True"
      ItemsSource="{Binding Source={StaticResource MyColors},XPath=color/@name}">
   </ListBox>
   <TextBlock Width="248" Height="24" Text="选中的颜色:" />
<TextBlock Width="248" Height="24" Text="{Binding ElementName=listXmlColor,  Path=SelectedValue, Mode=OneWay}"></TextBlock>


Colors.xml代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<colors >
  <color name="Pink"/>
  <color name="Red"/>
  <color name="Purple"/>
  <color name="Cyan"/>
  <color name="Gray"/>
  <color name="Turquoise"/>
</colors>


最终代码如下:

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="200" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="150"/>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0">
            <!--Resources一定要放在ListBox前面-->
            <StackPanel.Resources>
                <XmlDataProvider x:Key="MyColors" Source="Colors.xml"  XPath="colors"></XmlDataProvider>
            </StackPanel.Resources>
            
            <TextBlock Width="248" Height="24" Text="XML数据绑定:" TextWrapping="Wrap"/>
            <ListBox x:Name="listXmlColor" Width="248" Height="56" IsSynchronizedWithCurrentItem="True"
                     ItemsSource="{Binding Source={StaticResource MyColors},XPath=color/@name}">
            </ListBox>
            <TextBlock Width="248" Height="24" Text="选中的颜色:" />
            <TextBlock Width="248" Height="24" Text="{Binding ElementName=listXmlColor,  Path=SelectedValue, Mode=OneWay}"></TextBlock>
        </StackPanel>
    </Grid>
</Window>


结果如下图:


五、对象绑定和数据模板

       虽然 XmlDataProvider 对 XML 非常有用,但是当您想绑定到对象或对象列表时,可以创建 ObjectDataProvider 作为资源。ObjectDataProvider 的 ObjectType 指定将提供数据绑定源的对象,而 MethodName 则指示为获得数据而需调用的方法。例如,假设我有一个名为 StudentService 的类,该类使用一种名为 GetStudentList的方法来返回列表 

<ObjectDataProvider x:Key="students" ObjectType="{x:Type local:StudentService}" MethodName="GetStudentList">
</ObjectDataProvider>


       ObjectDataProvider 还可以使用许多其他属性。ConstructionParameters 属性允许您将参数传递给要调用的类的构造函数。此外,可以使用 MethodParameters 属性来指定参数,同时还可以使用 ObjectInstance 属性来指定现有的对象实例作为源。

       如果希望异步检索数据,可以将 ObjectDataProvider 的 IsAsynchronous 属性设为 true。这样,用户将可以在等待数据填充绑定到 ObjectDataProvider 的源的目标控件时与屏幕进行交互。

       在添加 ObjectDataProvider 时,必须限定数据源类的命名空间。在本例中,我必须将 xmlns 属性添加到 

xmlns:local="clr-namespace:WpfApp.Services"


       既然数据源已通过 ObjectDataProvider 定义,接下来就是如何将数据显示在 ListBox 控件。我要把姓名、年龄、出生日期、国籍在每个 ListBoxItem 中一行显示。姓名用粗体,年龄、出生日期、国籍使用默认字体显示。这在 XAML 中,通过使用数据模板(DataTemplate)很容易实现的,DataTemplate 允许您定义自己的显示样式。

       如下代码,在XAML代码中我将 DataTemplate 定义成如何显示Student信息的布局样式。我通过设置 DataTemplate 的 DataType 属性为students,告诉 DataTemplate 将要引用 Student类型。

       我将对象数据students绑定到 ListBox 的 ItemsSource 属性,这样就把将数据绑定到 ListBox了,但是我没有指定如何显示绑定的数据,显示样式是通过将 ItemTemplate 属性设置为 studentLayout资源(即 DataTemplate 的键名),就可以根据我之前在模板中设计的显示样式显示数据了。最终代码如下:

MainWindow.xaml 代码如下:

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp.Services"
        Title="MainWindow" Height="200" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="150"/>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0">
            <StackPanel.Resources>
                <ObjectDataProvider x:Key="students" ObjectType="{x:Type local:StudentService}" MethodName="GetStudentList"></ObjectDataProvider>
                <DataTemplate x:Key="studentLayout" DataType="students">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="Blue"/>
                        <TextBlock Text=", "></TextBlock>
                        <TextBlock Text="{Binding Path=Age}"></TextBlock>
                        <TextBlock Text=", "></TextBlock>
                        <TextBlock Text="{Binding Path=Birthday}"></TextBlock>
                        <TextBlock Text=", "></TextBlock>
                        <TextBlock Text="{Binding Path=Country}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </StackPanel.Resources>
            <TextBlock Width="248" Height="24" Text="对象数据绑定:" TextWrapping="Wrap"/>
            <ListBox x:Name="listObjectBind" Width="450" Height="100" IsSynchronizedWithCurrentItem="True"
                     ItemsSource="{Binding Source={StaticResource students}}"
                     ItemTemplate="{DynamicResource studentLayout}">
            </ListBox>
        </StackPanel>
    </Grid>
</Window>


StudentService.cs 代码如下:

using System.Collections.Generic;

namespace WpfApp.Services
{
    public class Student
    {
        public string Age { get; set; }
        public string Name { get; set; }
        public string Birthday { get; set; }
        public string Country { get; set; }
    }

    public class StudentService
    {
        public List<Student> GetStudentList()
        {
            Student liang = new Student();
            liang.Age = "18";
            liang.Name = "梁丘";
            liang.Birthday = "1990-02-03";
            liang.Country = "中国";
            Student zuo = new Student();
            zuo.Age = "22";
            zuo.Name = "左丘";
            zuo.Birthday = "1992-02-03";
            zuo.Country = "中国";
            Student diwu = new Student();
            diwu.Age = "32";
            diwu.Name = "第五言";
            diwu.Birthday = "1982-11-03";
            diwu.Country = "中国";
            Student yang = new Student();
            yang.Age = "12";
            yang.Name = "羊舌微";
            yang.Birthday = "2002-11-13";
            yang.Country = "中国";
            List<Student> personList = new List<Student>();
            personList.Add(liang);
            personList.Add(zuo);
            personList.Add(diwu);
            personList.Add(yang);
            return personList;
        }
    }
}


效果如下: