WPF 非元素类绑定Binding之 Source 属性
其用来绑定具体的数据对象:如系统信息跟我们定义的资源数据。
静态资源:
<Window.Resources>
<SolidColorBrush x:Key="redBrush">Red</SolidColorBrush>
</Window.Resources>
XAML:
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=Source}" Width="100" />
<TextBlock x:Name="txbSet" Width="100" Margin="0 10 0 0" Text="{Binding Source={StaticResource redBrush}}" Foreground="{Binding Source={StaticResource redBrush}}" />
</StackPanel>
View:
将 XAML 代码改成可读写的 TextBox:
<!--这个会报错-->
<TextBox Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=Source}" Width="100" />
<!--这个需要加 Path 路径-->
<TextBox Width="100" Margin="0 10 0 0" Text="{Binding Source={StaticResource redBrush}, Path=Color}" Foreground="{Binding Source={StaticResource redBrush}}" />
报错内容:
无法对“System.Windows.Media.FontFamily”类型的只读属性“Source”进行 TwoWay 或 OneWayToSource 绑定。
额外测试:
在 CodeBehind 中更改Resources["redBrush"] = new SolidColorBrush(Colors.White);
<TextBlock x:Name="txbSet" Width="100" Margin="0 10 0 0" Text="{Binding Source={StaticResource redBrush}, Path=Color,Mode=TwoWay}" Foreground="{StaticResource redBrush}" />
上面的方式不能像依赖属性那样的双向绑定自动变更值。Text 和 ForeGround 的值还是原来的。静态资源不具有绑定自动更新功能?
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了