当前位置:网站首页>WPF binding expression and binding data source (I)
WPF binding expression and binding data source (I)
2022-06-25 10:21:00 【@@Mr.Fu】
About binding :[Binding]
binding : It describes a relationship , Isomorphism is a relationship that connects multiple things .
Properties of page objects ( Must be a dependent property ): The goal is Target
Data objects that need to be displayed on the interface for interactive association : Source Source
<Window x:Class="WpfApp2.BindingDemo.Window1" 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:WpfApp2.BindingDemo" mc:Ignorable="d" Title="Window1" Height="450" Width="800"> <Grid> <TextBlock Text="{Binding Source=data,Path=value}"></TextBlock> </Grid> </Window> // remarks : //Text: The goal is //Source=data,Path=value Source : It's also a binding expression
1、 Binding expression
Text="{Binding Source=data,Path=value}" : Connect a property of the page object with the data source .
The interface can be isolated from the data logic through binding .
2、 Bind data source
1、 Specify the way :
Source、ElementName、DataContext、RelativeSource、Path、XPath
2、 Data source type
1) Dependent objects as data sources
<Window x:Class="WpfApp2.MainWindow"
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:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Name="window">
<Grid>
<StackPanel>
<!-- Dependent object binding -->
<TextBox Name="txt" Text="123"></TextBox>
<TextBlock Text="{Binding ElementName= txt,Path=Text}"></TextBlock>
<TextBlock Text="{Binding ElementName= window,Path=Title}"></TextBlock>
<TextBlock Text="Solider Current value :"></TextBlock>
<TextBlock Text="{Binding ElementName=slider,Path= Value}"></TextBlock>
<Slider Width="200" Name="slider" Minimum="5" Maximum="100" HorizontalAlignment="Left"></Slider>
<TextBlock Text="Solider minimum value :"></TextBlock>
<TextBlock Text="{Binding ElementName=slider,Path=Minimum}"></TextBlock>
</StackPanel>
</Grid>
</Window>
2) Common data type or collection type as data source
<Window.Resources>
<sys:String x:Key="name"> Xiao Ming </sys:String>
<x:Array x:Key="data" Type="sys:String">
<sys:String> Xiaogang </sys:String>
</x:Array>
</Window.Resources>
<!-- Common object , Collection object binding Start -->
<TextBlock Text="{Binding Source={StaticResource name}}"></TextBlock>
<TextBlock Text="{Binding Source={StaticResource data},Path=[0]}"></TextBlock>
<!-- Common object , Collection object binding end -->
3) Single object as data source ,INotifyPropertyChanged
The new class :DataClass Inherit INotifyPropertyChanged
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp2
{
public class DataClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
private string _Value;
public string Value
{
get { return _Value; }
set
{
_Value = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
}
}
}
}
xaml Code :
<Window.Resources>
<local:DataClass Value=" This is a single object as a data source ,INotifyPropertyChanged " x:Key="dataclass"></local:DataClass>
</Window.Resources>
<Window>
<!-- Single object as data source ,INotifyPropertyChanged Start -->
<TextBlock Text="{Binding Source={StaticResource dataclass},Path=Value}"></TextBlock>
<!-- Single object as data source ,INotifyPropertyChanged end -->
</Window>
xaml.cs Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
namespace WpfApp2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Task.Run(() => {
Task.Delay(2000).GetAwaiter().GetResult();
this.Dispatcher.Invoke(() => {
(this.Resources["dataclass"] as DataClass).Value = "";
});
});
}
}
}
4)XmlDataProvider As a data source
Create a new one XML file :【simple.xml】
<?xml version="1.0" encoding="iso-8859-1"?>
<breakfast_menu>
<food attr="a1" prop="p1">
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food attr="a2">
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food attr="a3">
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food attr="a4">
<name>French Toast</name>
<price>$4.50</price>
<description>thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food attr="a5">
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
xaml The code is as follows :
//d The first way to read Read object properties <Window.Resources> <XmlDataProvider x:Key="xmlData" Source="pack://application:,,,/WpfApp2;component/simple.xml" XPath="breakfast_menu/food[1]"> </XmlDataProvider> </Window.Resources> <Window> <!--xml Data binding Start --> <TextBlock Text="{Binding Source={StaticResource xmlData},XPath= name}"></TextBlock> <!--xml Data binding end --> </Window> // The second way to read Read object properties <Window.Resources> <XmlDataProvider x:Key="xmlData" Source="pack://application:,,,/WpfApp2;component/simple.xml"> </XmlDataProvider> </Window.Resources> <Window> <!--xml Data binding Start --> <TextBlock Text="{Binding Source={StaticResource xmlData},XPath= breakfast_menu/food[1]/name}"></TextBlock> <!--xml Data binding end --> </Window>// remarks :xml The multi node subscript of the file is from 1 At the beginning .
5)ObjectDataProvider As a data source
The new class :【MethodClass】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp2
{
public class MothedClass
{
public double GetValue(string value)
{
return double.Parse(value) * 0.5;
}
}
}
xmal The code is as follows :
<Window x:Class="WpfApp2.MainWindow"
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:WpfApp2"
xmlns:sys="clr-namespace:System;assembly=System.Runtime"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ObjectDataProvider ObjectType="{x:Type local:MothedClass}" x:Key="dataobject" MethodName="GetValue">
<ObjectDataProvider.MethodParameters>
<sys:String>
200
</sys:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<Border BorderBrush="Black" BorderThickness="1" Height="{Binding Source={StaticResource dataobject}}" Width="{Binding Source={StaticResource dataobject}}">
<TextBlock Text="{Binding Source={StaticResource dataobject}}"></TextBlock>
</Border>
</Grid>
</Window>
6) Static object properties as data sources
New static class :[StaticClass]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp2
{
public class StaticClass
{
public static int MyProperty { get; set; } = 555;
// notice The first way [Value]Changed Must correspond to the attribute name one by one 【 Not recommended 】
public static event EventHandler<PropertyChangedEventArgs> ValueChanged;
// notice The second way
public static event EventHandler<PropertyChangedEventArgs> StaricPropertyChanged;
private static int value { get; set; }=66;
public static int Value
{
get { return value; }
set { Value = value;
// notice The first way 【 Not recommended 】
// ValueChanged?.Invoke(null,new PropertyChangedEventArgs("Value"));
// notice The second way
StaricPropertyChanged?.Invoke(null, new PropertyChangedEventArgs("Value"));
}
}
}
}
xaml: The code is as follows
<Window x:Class="WpfApp2.MainWindow"
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:WpfApp2"
xmlns:sys="clr-namespace:System;assembly=System.Runtime"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Name="window">
<Grid>
<StackPanel>
<!-- Static object property binding -->
<!-- Single binding value -->
<TextBlock Text="{Binding Path=(local:StaticClass.MyProperty)}"></TextBlock>
<!-- Notify the attribute change when the attribute value changes -->
<TextBlock Text="{Binding Path=(local:StaticClass.Value)}"></TextBlock>
</StackPanel>
</Grid>
</Window>
边栏推荐
- Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days
- Grabcut image segmentation in opencv
- Neat Syntax Design of an ETL Language (Part 2)
- Force buckle -104 Maximum depth of binary tree
- 【论文阅读|深读】LINE: Large-scale Information Network Embedding
- MySQL create given statement
- i++ 和 ++i的真正区别
- The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
- How to make a self-made installer and package the program to generate an installer
- [dynamic planning] - Digital triangle
猜你喜欢

如何在Microsoft Exchange 2010中安装SSL证书

The title of my composition is - "my district head father"

Methodchannel of flutter

Learning notes of rxjs takeuntil operator

Request&Response有这一篇就够了

Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?

Redis(二)分布式锁与Redis集群搭建

The left sliding menu +menu item icon is grayed out

Wearable devices may reveal personal privacy

Free applet making tool, how to make wechat applet
随机推荐
Flutter Gaode map privacy compliance error
Summary of considerations for native applet development
Houdini图文笔记:Could not create OpenCL device of type (HOUDINI_OCL_DEVICETYPE)问题的解决
Processing picture class library
Free platform for wechat applet making, steps for wechat applet making
Modbus protocol and serialport port read / write
ShardingSphere-Proxy 4.1 分库分表
【论文阅读|深度】Role-based network embedding via structural features reconstruction with degree-regularized
Macro application connector\
How to apply for a widget on wechat how to get a widget on wechat
[RPC] i/o model - Rector mode of bio, NiO, AIO and NiO
WPF 绑定表达式和绑定数据源(一)
How to make small programs on wechat? How to make small programs on wechat
The left sliding menu +menu item icon is grayed out
Principle of distribution: understanding the gossip protocol
How to make a self-service order wechat applet? How to do the wechat order applet? visual editing
CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded
P2P network core technology: Gossip protocol
Kotlin advanced generic
Jetpack compose layout (I) - basic knowledge of layout