当前位置: 首页 > java >正文

WPF中Binding

image-20250429092124586

绑定ViewModel中的数据

添加数据上下文

方法一:在XAML中添加

<Window.DataContext><local:MainWindowViewModel />
</Window.DataContext>

方法二:在界面层的cs文件中添加

this.DataContext = new MainWindowViewModel();

绑定

public string Message { get; set; } = "Hello World";
<TextBlock Text="{Binding Path=Message}" Margin="10"/>
//简写此一个参数默认是Path
<TextBlock Text="{Binding Message}" Margin="10"/>
//什么都不屑默认绑定DataContext
<TextBlock Text="{Binding}" Margin="10"/>

绑定资源字典

简单的绑定资源字典的数据

<Window.DataContext><local:MainWindowViewModel />
</Window.DataContext>
<Window.Resources><sys:String x:Key="str">Hello, World</sys:String>
</Window.Resources>
<Grid><TextBlock Text="{Binding Source={StaticResource str}}" Margin="10"/>//简写<TextBlock Text="{staticResource str}" Margin="10"/>//绑定动态资源只能这样简写,写完整会报错<TextBlock Text="{DynamicResource str}" Margin="10"/>
</Grid>

绑定cs文件中资源字典的数据

先定义一个类

class MyResourece
{public string Message { get; } = "Public Property";public static string StaticString = "Static String";public const string ConstString = "Const String";
}

把类读取到资源字典

<Window.Resources><local:MyResourece x:Key="mysource" />
</Window.Resources>

对于静态,常量,enum,都得用x:Staic来访问,普通的资源可以用StaticResource

<TextBlock Text="{Binding Source={StaticResource mysource},Path=Message}"/>
//静态和常量得用x:Static访问
<TextBlock Grid.Row="1" Text="{Binding Source={x:Static local:MyResourece.ConstString}}" />
<TextBlock Grid.Row="2" Text="{Binding Source={x:Static local:MyResourece.StaticString}}" />

绑定控件

ElementName

<StackPanel><TextBox x:Name="txt" /><TextBlock Text="{Binding ElementName=txt, Path=Text}" />
</StackPanel>

x:Reference

当ElementName失效时可以用x:Reference,这是通用什么情况下都可以用

TextBlock Text="{Binding Source={x:Reference Name=txt},Path=Text}" />

RelativeSource

<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window},Path=Top}" />

我想读取到tag1的写法,当有多个相同的属性可以用AncestorLevel=3,标识找到父级第三个Grid

<Grid Tag="tag1"><Grid Tag="tag2"><Grid Tag="tag3"><TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid,AncestorLevel=3},Path=Tag}" /></Grid></Grid>
</Grid>

用ReletiveSource绑定同级别控件

<StackPanel><TextBox /><TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=StackPanel},Path=Children[0].Text}" />
</StackPanel>

绑定自己的写法四种写法

<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualWidth}" />
<TextBlock Text="{Binding RelativeSource={RelativeSource Self},Path=ActualWidth}" />
<TextBlock Text="{Binding RelativeSource={x:Static RelativeSource.Self},Path=ActualWidth}" />
<TextBlock Text="{Binding RelativeSource={RelativeSource 2},Path=ActualWidth}" />

绑定模板的属性,两种先给发等价

<StackPanel><Label Padding="10"><Label.Template><ControlTemplate TargetType="Label"><Border Padding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=Padding}"><ContentPresenter /></Border></ControlTemplate></Label.Template></Label>
</StackPanel>
<StackPanel><Label Padding="10"><Label.Template><ControlTemplate TargetType="Label"><Border><ContentPresenter Margin="{TemplateBinding Padding}" /></Border></ControlTemplate></Label.Template></Label>
</StackPanel>

StringFormat

StringFormat可以指定格式,:F3表示小数点后三位小数

<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=Top,StringFormat='Top:{0:F3}'}" />

MultiBinding

<StackPanel><TextBlock><TextBlock.Text><MultiBinding StringFormat="Pos:{0},{1}"><Binding Path="Top"RelativeSource="{RelativeSource AncestorType=Window}" /><Binding Path="Left"RelativeSource="{RelativeSource AncestorType=Window}" /></MultiBinding></TextBlock.Text></TextBlock>
</StackPanel>

有Content属性的控件,可以用ContentStringFormat

<StackPanel><Label Content="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=Top}"ContentStringFormat="Top:{0:F3}" />
</StackPanel>

FallbackValue和TargetNullValue

FallbackValue在没有绑定成功时优先显示

TargetNullValue在绑定的值为空时优先显示

<Label Content="{Binding Message,FallbackValue=dasdaa,TargetNullValue=asdasd}"/>

对比

RelativeSource、ElementName、x:Reference中前两者依赖对象树寻找关系,x:Reference可以从文档搜索,前两者绑定失效可以用x:Reference

x:Reference的缺陷,无法绑定自己的父级

BindingProxy

public class BindingProxy : Freezable
{#region Overrides of Freezableprotected override Freezable CreateInstanceCore(){return new BindingProxy();}#endregionpublic object Data{get { return (object)GetValue(DataProperty); }set { SetValue(DataProperty, value); }}// Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...public static readonly DependencyProperty DataProperty =DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
<Window.Resources><local:MyResourece x:Key="mysource" /><local:BindingProxy x:Key="mybtn"Data="{Binding ElementName=btn}" />
</Window.Resources>
<StackPanel><Button Name="btn"Content="Click Me"><Button.ToolTip><ToolTip><StackPanel><TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=Button},Path=Content}" /><TextBlock Text="{Binding Source={StaticResource mybtn},Path=Data.Content}" /></StackPanel></ToolTip></Button.ToolTip></Button>
</StackPanel>
http://www.xdnf.cn/news/3759.html

相关文章:

  • 【SimSession】1:将视频发送逻辑与 libuv 事件循环集成是一个典型的并发设计问题
  • 【论文阅读】LLMOPT:一种提升优化泛化能力的统一学习框架
  • 【leetcode】队列 + 宽搜,树形结构层序遍历的基础与变化
  • 短信侠 - 自建手机短信转发到电脑上并无感识别复制验证码,和找手机输验证码说再见!
  • 第四节:OpenCV 基础入门-第一个 OpenCV 程序:图像读取与显示
  • 五四青年节|模糊的青春岁月,用视频高清修复工具,让回忆更清晰!
  • 如何提升个人的思维能力?
  • 学习黑客环境配置
  • c++ 指针参数传递的深层原理
  • [Vue]props解耦传参
  • 我写了一个分析 Linux 平台打开文件描述符跨进程传递的工具
  • 动态规划之多状态问题1
  • AIStarter开发者手记:一键部署本地大模型,跨平台整合包技术解析
  • 63常用控件_QSlider的使用
  • STL之list容器
  • 计算机基础:二进制基础17,八进制减法
  • 大模型中常见的精度类型及区别​
  • 论微服务架构及其应用
  • 传奇各职业/战士/法师/道士/勋章爆率及出处产出地
  • 54、【OS】【Nuttx】编码规范解读(二)
  • 130. 被围绕的区域
  • (1)大模型的提示词工程实践技巧---LLM输出配置详解
  • 数字孪生赋能智慧城市:从概念到落地的深度实践
  • 【文献阅读】中国湿地随着保护和修复的反弹
  • DeepSeek眼中的文明印记:金刚经
  • 004 树与二叉树:从原理到实战
  • Baklib赋能企业知识管理数字化转型
  • MCP 协议知识分享指南
  • VS调试技巧
  • 网站即时备份,网站即时备份的方法有哪些