如何在代码中添加以下资源?
<Window.Resources>
<ResourceDictionary>
<FrameworkElement x:Key="OpenHand" Cursor="pack://application:,,,/Resources/openhand.cur"/>
</ResourceDictionary>
</Window.Resources>
答案 0 :(得分:5)
在C#Code-Behind中,您可以这样做:
ResourceDictionary rd = new ResourceDictionary();
FrameworkElement fe = new FrameworkElement()
{
Cursor = new Cursor("pack://application:,,,/Resources/openhand.cur")
};
rd.Add("OpenHand", fe);
Application.Current.Resources = rd;
如果您的资源中有其他ResourceDictionary,您应将rd添加到资源,而不是 将资源设置为rd :
Application.Current.Resources.MergedDictionaries.Add(rd);
答案 1 :(得分:1)
试试这个:
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
var res = new ResourceDictionary();
var frame = new FrameworkElement()
{
Cursor = new Cursor("pack://application:,,,/Resources/openhand.cur")
};
res .Add("framework", frame);
this.Resources.Add(rd);
}
答案 2 :(得分:-1)
var element = Application.Current.MainWindow.Resources["OpenHand"] as FrameworkElement;