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

相关推荐
huaqianzkh15 小时前
WMI 查询入门到实战:从概念、WQL 语法到 C# 采集系统与硬件信息完整示例
开发语言·c#
qq_4523962315 小时前
第十篇:分布式事务架构如何设计?从 Outbox 到 Saga 的生产级选型指南
分布式·架构·wpf
23957304918 小时前
Avalonia 12 代码编辑器性能横评:TKCode.Arc CodeEditor vs AvaloniaEdit 12.0(含深度调优实战)
c#·.net·avalonia·codeeditor·tkcode.arc
郝学胜-神的一滴1 天前
Effective STL 条款15:注意字符串实现上的差异
java·开发语言·数据结构·c++·算法·visualstudio
张人玉3 天前
C#如何使用FreeSql连接数据库
数据库·c#·freesql
吴可可1233 天前
C#打断多段线实现方法
c#
Kari113 天前
医药和制造行业的外文文档批量翻译,排版怎么保住?
开发语言·c#·制造
Warson_L4 天前
.NET 8 速成笔记 1:用 VS Code 做 C# / .NET 开发
c#·.net
Warson_L4 天前
.NET 8 速成笔记 9:C# 项目常见场景(基础设施篇)
c#·.net
Warson_L4 天前
.NET 8 速成笔记 8:C# 项目常见业务场景与写法(数据与业务篇)
c#·.net