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

相关推荐
工藤新一OL19 分钟前
把xml的格式从utf-8-bom转为utf-8
xml·c#·asp.net·.netcore·visual studio
henreash1 小时前
NLua和C#交互
开发语言·c#·交互
SAJalon5 小时前
C#集合全面解析
c#
SAJalon6 小时前
C#数组全面解析
c#
henreash7 小时前
xLua和C#交互
开发语言·c#·交互
慢慢沉16 小时前
C#(基本语法)
c#
★YUI★19 小时前
学习游戏制作记录(克隆技能)7.25
学习·游戏·unity·c#
坚持吧202120 小时前
【无标题】word 中的中文排序
开发语言·c#
_oP_i20 小时前
c# openxml 打开加密 的word读取内容
开发语言·c#·word
醉酒的李白、21 小时前
C#观察者模式示例代码
观察者模式·c#