WPF 非元素类绑定Binding之 Source 属性

2021/8/11 23:06:45

本文主要是介绍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 绑定。



参考:

https://www.cnblogs.com/wzh2010/p/6425060.html



这篇关于WPF 非元素类绑定Binding之 Source 属性的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程