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 小时前
02-06-历史-托管GC机制-垃圾回收如何影响数据结构选择
c#·编译·gc·机器码
Chris _data4 小时前
WPF 上位机开发学习笔记 - 第四天
笔记·学习·wpf
longxiaozhang611 小时前
C# static:静态成员,小白也能看懂
开发语言·c#
longxiaozhang611 小时前
C#继承:让代码少写一点,偷懒的好方法
开发语言·c#
rockey62712 小时前
C#脚本引擎之AScript与DynamicExpresso对比
c#·.net·script·动态脚本
李高钢12 小时前
用 JetBrains Rider 写 WPF 的 15 个高效技巧:从 VS 转过来也值得看
wpf
Vae_Mars1 天前
C#中的delegate委托
开发语言·c#
公子小六1 天前
基于.NET的Windows窗体编程之WinForms音频控件
windows·microsoft·c#·.net·音视频·winforms
绿浪19841 天前
c# 结构体 能不能直接封送检测
开发语言·c#
新手unity自用笔记1 天前
unity基于Socket的网络学习
网络·网络协议·学习·unity·c#·游戏引擎