WPF 绑定StaticResource到控件的方法
資源文件內的屬性能否直接通過綁定應用到控件?答案是肯定的。
比如,我們要直接把下面的<SolidColorBrush x:Key="RedBrush" Color="#FFFF0000" />直接綁定到一個TextBlock的Foreground屬性。
<Application x:Class="StaticResourcesWithBinding.App"
? ? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
? ? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
? ? StartupUri="Window1.xaml">
? ? <Application.Resources>
<SolidColorBrush x:Key="RedBrush" Color="#FFFF0000" />
? ? </Application.Resources>
</Application>
辦法是直接把資源文件內的Key的名字綁定到控件,
public class MyViewModel
{
public string MyResourceKey { get; private set; }
public MyViewModel(string myResourceKey)
{
MyResourceKey = myResourceKey;
}
}
直接綁定可以嗎?
<Window x:Class="StaticResourcesWithBinding.Window1"
? ? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
? ? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
???Title="Window1" Width="400" Height="200">
<Grid>
<TextBlock Text="Hello World" FontSize="48" Foreground="{StaticResource ?{Binding MyResourceKey}}" />
</Grid>
</Window>
但這樣是行不通的。
必須要用下面的類進行轉換
using System; using System.Windows; using System.Windows.Data; using System.Windows.Markup;namespace StaticResourcesWithBinding {public class BindableStaticResource : StaticResourceExtension{private static readonly DependencyProperty DummyProperty;static BindableStaticResource(){DummyProperty = DependencyProperty.RegisterAttached("Dummy",typeof(Object),typeof(DependencyObject),new UIPropertyMetadata(null));}public Binding MyBinding { get; set; }public BindableStaticResource(){}public BindableStaticResource(Binding binding){MyBinding = binding;}public override object ProvideValue(IServiceProvider serviceProvider){var target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));var targetObject = (FrameworkElement)target.TargetObject;MyBinding.Source = targetObject.DataContext;var DummyDO = new DependencyObject();BindingOperations.SetBinding(DummyDO, DummyProperty, MyBinding);ResourceKey = DummyDO.GetValue(DummyProperty);return ResourceKey != null ? base.ProvideValue(serviceProvider) : null;}public new object ResourceKey{get{return base.ResourceKey;}set{if (value != null){base.ResourceKey = value;}}}} }然后,我們就可以通過以下方式綁定了:
<Window x:Class="StaticResourcesWithBinding.Window1"
? ? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
? ? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
? ? xmlns:StaticResourcesWithBinding="clr-namespace:StaticResourcesWithBinding"
? ? Title="Window1" Width="400" Height="200">
<Grid>
<TextBlock Text="Hello World" FontSize="48" Foreground="{StaticResourcesWithBinding:BindableStaticResource {Binding MyResourceKey}}" />
</Grid>
</Window>
后面的代碼:
public partial class Window1
{
public Window1()
{
? ? ? ? ? ? this.DataContext = new MyViewModel("RedBrush");
InitializeComponent();
? ? ? ? ? ??
}
}
本文完整源碼下載
轉載于:https://www.cnblogs.com/lonelyxmas/p/10711565.html
總結
以上是生活随笔為你收集整理的WPF 绑定StaticResource到控件的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习笔记02:直播串讲——3/22
- 下一篇: .NET Framework学习笔记(十