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

相关推荐
曹牧1 小时前
C#:主线程能够捕获到子线程中的异常
开发语言·数据库·c#
LCG元6 小时前
现代Web应用高可用架构设计与性能调优实战
前端·wpf
jiayong237 小时前
海量数据处理技术方案与实现原理
大数据·c#·linq
小二·11 小时前
向量数据库深度对比:PGVector vs Qdrant vs Milvus vs Chroma(附性能测试数据)
数据库·wpf·milvus
z落落11 小时前
C# 类与对象、字段、静态与非静态+四大访问修饰符
开发语言·c#
魔法阵维护师11 小时前
从零开发游戏需要学习的c#模块,第三十二章(Boss 战系统)
学习·游戏·c#
魔法阵维护师12 小时前
从零开发游戏需要学习的c#模块,第三十三章(暂停菜单)
学习·游戏·c#
z落落13 小时前
C# Stack栈 / Queue队列+所有集合 终极一页汇总(全覆盖、零遗漏)
java·开发语言·c#
淡海水14 小时前
08-认知篇-对比-injectfix深度解析
unity·c#·热更新·clr·hybrid·injectfix