0
点赞
收藏
分享

微信扫一扫

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:

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

将 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 的值还是原来的。静态资源不具有绑定自动更新功能?




举报

相关推荐

0 条评论