WPF绑定Bind方法合集,实时更新

绑定静态属性x:Static

xml 复制代码
<TextBlock Text="{x:Static System:Environment.MachineName}" />

绑定到自身的属性RelativeSource Self

xml 复制代码
<Rectangle Width="100" Height="{Binding Width, RelativeSource={RelativeSource Self}}" />

绑定到集合的当前项:你可以使用Path=Items/来绑定到集合的当前项

C# 复制代码
public List<string> strings { get; set; } = Enumerable.Repeat("Hello", 10).ToList();
xml 复制代码
<TextBlock Text="{Binding Path=Items/}" />

表现为第一个Hello,如果是IEnumerable类型则对应yield的顺序

获取集合当前项的第0个成员strings/[0]

C# 复制代码
public List<string> strings { get; set; } = Enumerable.Repeat("Hello", 10).ToList();
xml 复制代码
<TextBlock Text="{Binding strings/[0]}" />

获取当前项的第0个元素

表现为第一个Hello的第一个字母,H

特殊绑定(Button.Background).(SolidColorBrush.Color)

(Button.Background).(SolidColorBrush.Color)表示的是Button的Background属性的Color属性。Button.Background是一个Brush类型的属性,SolidColorBrush.Color是SolidColorBrush的一个属性,表示颜色。

由于Background是Brush类型,Brush是抽象类,SolidColorBrush是子类,意为把((SolidColorBrush)(Button.Background)).Color

相关推荐
卡拉叽里呱啦11 分钟前
C#中异步的用法、原则和基本原理
开发语言·c#
ghost1432 小时前
C#学习第18天:特性(Attributes)
开发语言·学习·c#
EanoJiang10 小时前
CSharp_base
c#
xiaowu08012 小时前
C# 使用Windows API实现键盘钩子的类
windows·c#·计算机外设
Java林间15 小时前
Zookeeper是什么?基于zookeeper实现分布式锁
分布式·zookeeper·wpf
十步杀一人_千里不留行15 小时前
面向 C# 初学者的完整教程
开发语言·c#
LcVong16 小时前
一篇文章学会开发第一个ASP.NET网页
后端·c#·asp.net·web
刘梦凡呀18 小时前
C# .NET Core 批量下载文件
windows·c#·.netcore
zizisuo19 小时前
1.微服务拆分与通信模式
微服务·wpf
码观天工1 天前
C#高性能开发之类型系统:从C# 7.0 到C# 14的类型系统演进全景
性能优化·c#·.net·memory·高性能·record·c#14·类型系统