日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF 绑定StaticResource到控件的方法

發布時間:2023/12/10 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF 绑定StaticResource到控件的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
WPF 綁定StaticResource到控件的方法 原文: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();
? ? ? ? ? ??
}
}


本文完整源碼下載


posted on 2019-04-15 16:54 NET未來之路 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/lonelyxmas/p/10711565.html

總結

以上是生活随笔為你收集整理的WPF 绑定StaticResource到控件的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。