关于数据绑定
Binding C# XAML {Binding} MarkupExtension
C# 演示:
Binding binding = new Binding();
XAML演示:
<TextBlock Text ="{Binding}" />
绑定:描述的是一种关系,通过某种关系将多个事物联系在一起
通常情况下,每个绑定具有四个组件
- 绑定目标对象。
- 目标属性。
- 绑定源。
- 指向绑定源中要使用的值的路径。
数据绑定的典型用法是将服务器或本地配置数据放置到窗体或其他 UI 控件中。不论要绑定什么元素,也不论数据源是什么性质,每个绑定都始终遵循下图所示的模型。
数据绑定中的数据源
指定方式:
DataContext、Source、ElementName、RelativeSource
Path、XPath
使用ElementName 和 Path数据绑定:
path代表的是路径,是数据源下面的路径,不是属性
<!--目标对象:TextBlock-->
<!--目标属性:Text-->
<!--绑定数据源:tb_source-->
<!--绑定路径:Text-->
<TextBox Text="Hello" Name="tb_source" Foreground="Red" />
<TextBox Text="{Binding ElementName=tb_source,Path=Text}" />
注意:
- 如果绑定的源是依赖对象的依赖属性,那么这个依赖属性发生实时变化的时候,会同步到绑定目标
- 如果绑定的源是非依赖属性,那么这个属性发生变化的时候,不会同步到绑定目标
- 被绑定的数据源,需要是属性
使用DataContext 和 Source 绑定数据源:
<Window x:Class="XH.BindingLesson.DataSource.NormalDataSourceWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XH.BindingLesson.DataSource"
mc:Ignorable="d" FontSize="30"
xmlns:sys="clr-namespace:System;assembly=netstandard"
Title="NormalDataSourceWindow" Height="450" Width="800">
<Window.Resources>
<sys:Int32 x:Key="Value">100</sys:Int32>
<sys:Int32 x:Key="V1">200</sys:Int32>
<!--定义一个数组-->
<x:Array x:Key="list" Type="sys:String">
<sys:String>AAAA</sys:String>
<sys:String>BBBB</sys:String>
<sys:String>CCCC</sys:String>
<sys:String>DDDD</sys:String>
</x:Array>
</Window.Resources>
<StackPanel>
<!--.表示数据源本身-->
<TextBlock Text="{Binding Source={StaticResource Value},Path=.}" x:Name="tb" />
<!--两种方式都可以-->
<TextBlock Text="{Binding Path = .}" DataContext="{StaticResource Value}" />
<!--如果Source 和 DataContext同时使用,优先使用Source的数据源,就近原则-->
<TextBlock Text="{Binding Source={StaticResource V1},Path=.}" DataContext="{StaticResource Value}" />
<!--1、目前两种形式的数据源
依赖对象(页面上的对象,指定名称),同过ElementName建立数据源联系
普通股数据源,演示是通过Source建立数据源联系,
同时可以使用目标对象的DataContext属性进行数据源的关联-->
<!--获取数据的绑定过程-->
<TextBlock Text="{Binding Source={StaticResource list},Path=[1]}" />
<ListBox ItemsSource="{Binding Source={StaticResource list}}" />
<ComboBox ItemsSource="{Binding Source={StaticResource list}}" />
</StackPanel>
</Window>
注意:
如果Source 和 DataContext同时使用,优先使用Source的数据源,就近原则
数据源类型:
依赖对象做为数据源
<TextBox Text="Hello" Name="tb_source" Foreground="Red" />
<TextBox Text="{Binding ElementName=tb_source,Path=Text}" />
演示:
普通数据类型或集合类型做为数据源
<Window x:Class="XH.BindingLesson.DataSource.NormalDataSourceWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XH.BindingLesson.DataSource"
mc:Ignorable="d" FontSize="30"
xmlns:sys="clr-namespace:System;assembly=netstandard"
Title="NormalDataSourceWindow" Height="450" Width="800">
<Window.Resources>
<sys:Int32 x:Key="Value">100</sys:Int32>
<sys:Int32 x:Key="V1">200</sys:Int32>
<!--定义一个数组-->
<x:Array x:Key="list" Type="sys:String">
<sys:String>AAAA</sys:String>
<sys:String>BBBB</sys:String>
<sys:String>CCCC</sys:String>
<sys:String>DDDD</sys:String>
</x:Array>
</Window.Resources>
<StackPanel>
<!--.表示数据源本身-->
<TextBlock Text="{Binding Source={StaticResource Value},Path=.}" x:Name="tb" />
<!--两种方式都可以-->
<TextBlock Text="{Binding Path = .}" DataContext="{StaticResource Value}" />
<!--如果Source 和 DataContext同时使用,优先使用Source的数据源,就近原则-->
<TextBlock Text="{Binding Source={StaticResource V1},Path=.}" DataContext="{StaticResource Value}" />
<!--1、目前两种形式的数据源
依赖对象(页面上的对象,指定名称),同过ElementName建立数据源联系
普通股数据源,演示是通过Source建立数据源联系,
同时可以使用目标对象的DataContext属性进行数据源的关联-->
<!--获取数据的绑定过程-->
<TextBlock Text="{Binding Source={StaticResource list},Path=[1]}" />
<ListBox ItemsSource="{Binding Source={StaticResource list}}" />
<ComboBox ItemsSource="{Binding Source={StaticResource list}}" />
</StackPanel>
</Window>
演示:
单个对象做为数据源
xaml代码:
<Window x:Class="XH.BindingLesson.DataSource.ObjectDataSourceWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XH.BindingLesson.DataSource"
mc:Ignorable="d"
Title="ObjectDataSourceWindow" Height="450" Width="800">
<Window.Resources>
<!--Person p = new Person();-->
<local:Person x:Key="p" Age="30" Name="Mic" />
<Button x:Key="button" Width="300"/>
</Window.Resources>
<StackPanel>
<!--用类当数据源-->
<!--可以使用下标,也可以使用属性名-->
<TextBlock Text="{Binding Source={StaticResource p},Path=Age}" />
<TextBlock Text="{Binding Source={StaticResource p},Path=Name}" />
<TextBlock Text="{Binding Source={StaticResource button},Path=Width}" />
</StackPanel>
</Window>
C#代码: 就创建一个对象类
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
演示:
ADO.NET数据对象做为数据源(MySql、SqlServer、Oracle、Sqlite)
XAML代码:
<Window x:Class="XH.BindingLesson.DataSource.ADODataSourceWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XH.BindingLesson.DataSource"
mc:Ignorable="d"
Title="ADODataSourceWindow" Height="450" Width="800">
<Grid>
<ListView x:Name="lv">
<ListView.View>
<GridView>
<GridViewColumn
Header="id"
Width="100"
DisplayMemberBinding="{Binding _id}"/>
<GridViewColumn
Header="name"
Width="100"
DisplayMemberBinding="{Binding name}" />
<GridViewColumn
Header="age"
Width="100"
DisplayMemberBinding="{Binding age}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
C#代码:创建ado.net 我这里用sqlite为例
using Microsoft.Data.Sqlite;
using SQLitePCL;
using System.Data;
using System.Windows;
namespace XH.BindingLesson.DataSource
{
/// <summary>
/// ADODataSourceWindow.xaml 的交互逻辑
/// </summary>
public partial class ADODataSourceWindow : Window
{
public ADODataSourceWindow()
{
InitializeComponent();
// 初始化 SQLitePCL 库
Batteries.Init();
string connectionString = "Data Source=test.db;";
string conStr = "Data Source=test.db";
using (SqliteConnection connection = new SqliteConnection(conStr))
{
connection.Open();
string query = "SELECT * FROM sys_user";
SqliteCommand command = new SqliteCommand(query, connection);
SqliteDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
this.lv.ItemsSource = dt.DefaultView;
}
}
}
}
需要下载以下,两个NuGet包
效果演示:
数据库内容演示:
Linq结果做为数据源 :List
在ado.net中进行修改为linq表达式:
XAML代码:
<Window x:Class="XH.BindingLesson.DataSource.LinqDataSource"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XH.BindingLesson.DataSource"
mc:Ignorable="d"
Title="LinqDataSource" Height="450" Width="800">
<Grid>
<ListView x:Name="lv">
<ListView.View>
<GridView>
<GridViewColumn
Header="id"
Width="100"
DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn
Header="name"
Width="100"
DisplayMemberBinding="{Binding Name}" />
<GridViewColumn
Header="age"
Width="100"
DisplayMemberBinding="{Binding Age}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
using Microsoft.Data.Sqlite;
using SQLitePCL;
using System.Data;
using System.Windows;
namespace XH.BindingLesson.DataSource
{
/// <summary>
/// LinqDataSource.xaml 的交互逻辑
/// </summary>
public partial class LinqDataSource : Window
{
public LinqDataSource()
{
InitializeComponent();
// 初始化 SQLitePCL 库
Batteries.Init();
string connectionString = $"Data Source=test.db;";
string conStr = "Data Source=test.db";
using (SqliteConnection connection = new SqliteConnection(conStr))
{
connection.Open();
string query = "SELECT * FROM sys_user";
SqliteCommand command = new SqliteCommand(query, connection);
SqliteDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
// 转换为匿名对象
// 如果不转换的话,xaml代码绑定的时候,需要用下标进行绑定
var result = from q in dt.AsEnumerable()
where q.Field<string>("name").ToString() == "张三"
select new
{
Id = int.Parse(q["_id"].ToString()),
Age = q.Field<string>("age").ToString(),
Name = q.Field<string>("name").ToString()
};
this.lv.ItemsSource = result;
}
}
}
}
效果演示:
注意:
如果在使用linq的时候,查询之后,没有转换为匿名对象,或者说没有转换为一个对象,则需要在xaml绑定的时候,需要指定下标进行绑定。
ObjectDataProvider做为数据源、
C#代码部分:写一个方法
public class MethodObject
{
public double Calculator(string value)
{
return double.Parse(value) * 100;
}
}
XAML地方调用:
<Window x:Class="XH.BindingLesson.DataSource.ObjectDataProviderWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XH.BindingLesson.DataSource"
mc:Ignorable="d"
xmlns:sys="clr-namespace:System;assembly=System.Runtime"
Title="ObjectDataProviderWindow" Height="450" Width="800">
<Window.Resources>
<!--{Source = 对象方法 Path = 方法} 资源标签-->
<ObjectDataProvider x:Key="odp"
ObjectType="{x:Type local:MethodObject}"
MethodName="Calculator">
<ObjectDataProvider.MethodParameters>
<sys:String>2.0</sys:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<StackPanel>
<TextBlock Text="{Binding Source={StaticResource odp},Path=.}"/>
<!--BindsDirectlyToSource 获取或设置一个值,该值指示是相对于数据项-->
<!--这个绑定特殊 特殊在绑定的源是ObjectDataProvider的参数
TextBox 绑定默认是String -->
<TextBox Text="{Binding Source={StaticResource odp},Path=MethodParameters[0],BindsDirectlyToSource=True}" />
<TextBox />
</StackPanel>
</Window>
效果:上面计算的数据可以根据我输入的内容而进行计算
XmlDataProvider做为数据源
<Window x:Class="XH.BindingLesson.DataSource.XMLDataProviderWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XH.BindingLesson.DataSource"
mc:Ignorable="d" FontSize="30"
Title="XMLDataProviderWindow" Height="450" Width="800">
<Window.Resources>
<!--前提是xml文件 xml字符串不行 必须是文件
必须是绝对路径-->
<XmlDataProvider x:Key="xdp"
Source="pack://application:,,,/XH.BindingLesson;component/DataSource/simple.xml">
</XmlDataProvider>
</Window.Resources>
<StackPanel>
<!--下标是从1开始的
XPath是路径 层级关系
绑定属性 : @ + 属性-->
<!--使用场景:
XML的数据配置,从文件加载出XML之后,在界面做显示
数据接口获取数据,数据绑定采用的XML结构,直接把数据绑定在界面进行显示
大部分Json:序列化与反序列化-->
<!--C#进行XML,需要XmlDocument进行处理,这个对象可以转Linq-->
<TextBlock Text="{Binding Source={StaticResource xdp},XPath=breakfast_menu/food[3]/@attr}"
TextWrapping="Wrap"
Visibility="Visible"/>
<TextBlock Text="{Binding Source={StaticResource xdp},XPath=breakfast_menu/food[1]/@prop}"
TextWrapping="Wrap"
Visibility="Visible"/>
<ListBox ItemsSource="{Binding Source={StaticResource xdp},XPath=breakfast_menu/food}"
Visibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="名称:" />
<TextBlock Grid.Column="1" Text="{Binding XPath=name}" />
<TextBlock Grid.Column="2" Text="单价:" />
<TextBlock Grid.Column="3" Text="{Binding XPath=price}" />
<TextBlock Grid.Column="4" Text="简介:" />
<TextBlock Grid.Column="5" Text="{Binding XPath=description}" />
<TextBlock Grid.Column="6" Text="热量:" />
<TextBlock Grid.Column="7" Text="{Binding XPath=calories}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Visibility="Visible">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="名称" />
<TextBlock Grid.Column="1" Text="单价" />
<TextBlock Grid.Column="2" Text="简介" />
<TextBlock Grid.Column="3" Text="热量" />
</Grid>
<ItemsControl ItemsSource="{Binding Source={StaticResource xdp},XPath=breakfast_menu/food}"
Visibility="Visible">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding XPath=name}" />
<TextBlock Grid.Column="1" Text="{Binding XPath=price}" />
<TextBlock Grid.Column="2" Text="{Binding XPath=description}" />
<TextBlock Grid.Column="3" Text="{Binding XPath=calories}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Window>
演示:
静态对象属性
XAML代码:
<!--静态属性的绑定方式 固定写法-->
<!--只会显示一次 后续字段刷新不会显示-->
<TextBlock Text="{Binding Source={x:Static local:GlobalDatas.Value},Path=.}"/>
<!--推荐 这种写法 可以绑定刷新-->
<TextBlock Text="{Binding Path=(local:GlobalDatas.Value)}"/>
C#代码:
public class GlobalDatas
{
public static string Value { get; set; } = "Hello Static";
}
DataContext
DataContext 可以写在任何标签里面,但是赋值遵循就近原则 也可以在cs文件中定义
C#代码定义:
// 依赖对象
//this.DataContext = this;
// 普通数据类型
//this.DataContext = "ABCD";
// 单个对象示例
this.DataContext = new Person()
{
Name="张三",
Age = 18
};
XAML代码:
<TextBlock Text="{Binding Age}" />
启动:
全部在XAML代码中使用:
<Window.DataContext>
<local:Person Name="Xiaohai" Age="30" />
</Window.DataContext>
<TextBlock Text="{Binding Age}" />
注意:如果所在标签内定义一个DataContext,而Window中也定义一个DataContext,他遵循就近原则
如下:
<Window x:Class="XH.BindingLesson.DataSource.DataContextWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XH.BindingLesson.DataSource"
mc:Ignorable="d" FontSize="30"
Title="DataContextWindow" Height="450" Width="800">
<Window.DataContext>
<local:Person Name="Xiaohai" Age="30" />
</Window.DataContext>
<Grid>
<!--DataContext也遵循就近原则-->
<Grid.DataContext>
<local:Person Name="Good" Age="23" />
</Grid.DataContext>
<StackPanel>
<TextBlock Text="{Binding Age}" />
</StackPanel>
</Grid>
</Window>
显示:
RelativeSource
需求:如果需要两个不同的数据源的时候,需要用RelativeSource进行定位:
写法:
<!--RelativeSource={RelativeSource }-->
<!--第一个是对象 第二个是示例 -->
Mode 属性:
FindAncestor
绑定到特定类型的上级
<TextBlock Text="{Binding Path=DataContext.Age,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
Visibility="Visible"
Foreground="Red" /
PreviousData
绑定上一个数据项
Self
绑定的是自身 使用Self的时候 "Mode =" 可以省略
<TextBlock Text="{Binding Path=Foreground,RelativeSource={RelativeSource Self}}"
Visibility="Visible"
Foreground="Orange" />
TemplatedParent
用于模板绑定 相当于TemplateBinding
<Window x:Class="XH.BindingLesson.DataSource.DataContextWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XH.BindingLesson.DataSource"
mc:Ignorable="d" FontSize="30"
Title="DataContextWindow" Height="450" Width="800">
<Window.Resources>
<!--TemplatedParent 模板对象
不是所有的绑定都可以用TemplateBinding-->
<ControlTemplate TargetType="Button" x:Key="btnTemp">
<Border Background="{Binding Path=Background,RelativeSource={RelativeSource Mode=TemplatedParent}}"
Width="{TemplateBinding Width}">
<!--<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>-->
<!--数据源要求可编辑,Text默认是双向绑定-->
<TextBox Text="{Binding ActualWidth,Mode=OneWay,RelativeSource={RelativeSource Mode=TemplatedParent}}" />
</Border>
</ControlTemplate>
</Window.Resources>
<StackPanel>
<Button Content="Button"
Visibility="Visible"
Background="Orange"
Template="{StaticResource btnTemp}"/>
</StackPanel>
</Window>
注意:不是所有的绑定都可以用TemplateBinding,如果数据源要求可编辑的话,不可以使用TemplateBinding
Demo
1. 案例:WPF,界面对象自适应,界面中有个控件,宽度希望是窗口的一半、三分之一、三分之二
xaml代码:
<Window x:Class="XH.BindingLesson.Example.AutoWidthWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XH.BindingLesson.Example"
mc:Ignorable="d"
Title="AutoWidthWindow" Height="450" Width="800">
<Grid Margin="30" x:Name="grid">
<!--ActualWidth 渲染宽度 一般绑定都指定这个宽度-->
<!--这个控件希望有三分之一的宽度
1、拿到Grid的三分之一宽度
2、放到Width中-->
<Grid ShowGridLines="False">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="cd"/>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border x:Name="bor" Grid.ColumnSpan="2"/>
<!--拉动窗格-->
<GridSplitter HorizontalAlignment="Right" Background="Red" Width="5" Visibility="Collapsed" />
</Grid>
<Border Background="Orange" Width="{Binding ElementName=bor,Path=ActualWidth}" Visibility="Visible"/>
</Grid>
</Window>
注意:ActualWidth 渲染宽度 一般绑定都指定这个宽度
2. 案例:多数据源的处理
XAML代码:
<Window x:Class="XH.BindingLesson.Example.MulitDataSourceWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XH.BindingLesson.Example"
mc:Ignorable="d" FontSize="30"
Title="MulitDataSourceWindow" Height="450" Width="800">
<!--多数据源案例-->
<Window.DataContext>
<local:DataSource />
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid>
<TextBlock Text="{Binding Path=A.Name}" Foreground="Red" />
</Grid>
<Grid Grid.Column="1">
<TextBlock Text="{Binding Path=B.Value}" Foreground="Red" />
</Grid>
</Grid>
</Window>
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XH.BindingLesson.Example
{
public class DataSourceA
{
public string Name { get; set; } = "Hello";
}
public class DataSourceB
{
public int Value { get; set; } = 100;
}
public class DataSource
{
public DataSourceA A { get; set; } = new DataSourceA();
public DataSourceB B { get; set; } = new DataSourceB();
}
}
显示如下:
多数据的逻辑关系: