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

相关推荐
Python大数据分析@1 天前
Python 语言有什么奇技淫巧吗?
开发语言·python·c#
Envyᥫᩣ1 天前
C#中的设计模式:构建更加优雅的代码
开发语言·c#
没有bug.的程序员1 天前
Redis 数据结构全面解析:从底层编码到实战应用
java·数据结构·redis·wpf
CHHC18801 天前
C#工作流示例(WorkflowCore)
c#·工作流
somethingGoWay1 天前
wpf 自定义输入ip地址的文本框
wpf
oscar9991 天前
少儿编程C++快速教程之——2. 字符串处理
开发语言·c++·c#·字符串·少儿
时光追逐者1 天前
.NET 使用 CsvHelper 快速读取和写入 CSV 文件
c#·.net·.net core·csv
INSO1 天前
查漏补缺之Polly
c#
秋月的私语1 天前
Wpf程序屏幕居中问题修复全记录
wpf
我要打打代码1 天前
WPF启动窗体的三种方式
wpf