Reputation: 5444
How can I add the below resource in code behind?
<Window.Resources>
<ResourceDictionary>
<FrameworkElement x:Key="OpenHand" Cursor="pack://application:,,,/Resources/openhand.cur"/>
</ResourceDictionary>
</Window.Resources>
Upvotes: 3
Views: 5362
Reputation: 8231
In C# Code-Behind, you can do like this:
ResourceDictionary rd = new ResourceDictionary();
FrameworkElement fe = new FrameworkElement()
{
Cursor = new Cursor("pack://application:,,,/Resources/openhand.cur")
};
rd.Add("OpenHand", fe);
Application.Current.Resources = rd;
If there are other ResourceDictionary in you Resources, you should add rd into Resources, not set Resources to rd:
Application.Current.Resources.MergedDictionaries.Add(rd);
Upvotes: 6
Reputation: 463
var element = Application.Current.MainWindow.Resources["OpenHand"] as FrameworkElement;
Upvotes: -1
Reputation: 5474
Try this :
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);
}
Upvotes: 2