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

相关推荐
来恩10033 小时前
C# 类与对象详解
开发语言·c#
Dr.勿忘6 小时前
C#面试常考随笔8:using关键字有哪些用法?
开发语言·unity·面试·c#·游戏引擎
xcLeigh6 小时前
WPF进阶 | WPF 数据绑定进阶:绑定模式、转换器与验证
c#·wpf
谢大旭6 小时前
ASP.NET Core 中间件
后端·中间件·c#
时光追逐者7 小时前
Visual Studio使用GitHub Copilot提高.NET开发工作效率
c#·github·.net·copilot·ai编程·微软技术·visual studio
唐青枫9 小时前
dotnet LINQ 使用简明教程
c#·.net
谢大旭10 小时前
.Net Web API 访问权限限定
开发语言·c#
幻想趾于现实12 小时前
如何用函数去计算x年x月x日是(C#)
开发语言·c#
我命由我123451 天前
游戏引擎 Unity - Unity 下载与安装
c语言·开发语言·c++·后端·unity·c#·游戏引擎
我命由我123451 天前
游戏引擎 Unity - Unity 启动(下载 Unity Editor、生成 Unity Personal Edition 许可证)
c语言·c++·后端·unity·c#·游戏引擎·ue4