I have a windowless application that only consists of an App.xaml which is filled by a ResourceDictionary. How can I access a control in that Dictionary from its codebehind?
Asked
Active
Viewed 1,575 times
1 Answers
3
After trying various methods that all didn't work, such as getting the control via VisualTreeHelper, accessing the control directly via name, the solution is surprisingly simple:
ResourceDictionary.xaml
<ResourceDictionary x:Class="My.Namespace"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button x:Key="myButtonName" />
</ResourceDictionary>
ResourceDictionary.xaml.cs:
// Example with a button control
Button myButton= this["myButtonName"] as Button;
if(myButton != null)
{
// Do something
}

Lennart
- 9,657
- 16
- 68
- 84
-
great find! happy coding :) – pushpraj Oct 17 '14 at 08:39
-
1Asked and answered within the same minute? You must have had the answer typed out before you even posted the question. – Tyler Pantuso Jan 07 '16 at 23:50