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

相关推荐
张人玉8 小时前
C#通讯(上位机)常用知识点
开发语言·c#·通讯·上位机开发
武藤一雄10 小时前
C#:nameof 运算符全指南
开发语言·microsoft·c#·.net·.netcore
CSharp精选营12 小时前
聊一聊 C# 中的闭包陷阱:foreach 循环的坑你还记得吗?
c#·foreach·循环·for循环
月巴月巴白勺合鸟月半12 小时前
FHIR 的使用
人工智能·c#·fhir
公子小六13 小时前
基于.NET的Windows窗体编程之WinForms控件简介
windows·microsoft·c#·.net
观无14 小时前
mysql5.7下载地址
c#
武藤一雄14 小时前
C# 核心技术解析:Parse vs TryParse 实战指南
开发语言·windows·microsoft·微软·c#·.netcore
代数狂人14 小时前
在Godot中应用面向对象原则:C#脚本实践
c#·游戏引擎·godot
白露与泡影14 小时前
Spring Cloud进阶--分布式权限校验OAuth2
分布式·spring cloud·wpf
斌味代码15 小时前
RAG 实战:用 LangChain + DeepSeek 搭建企业私有知识库问答系统
开发语言·langchain·c#