'Access constants from nested C# class in xaml
Let's say I have a class defined something like the below:
namespace MyProject.MyConstants
{
public class Constants
{
public class Group1Constants
{
public const string DoIt= "DoIt";
}
}
}
I am trying to use this const, from a separate project, in my xaml. I included the namespace:
xmlns:constants="clr-namespace:MyProject.MyConstants;assembly=MyProject.MyConstants"
and am trying to use the constant as follows:
<MenuItem Header="{x:Static controls:Constants.Group1Constants.DoIt}">
The above wont compile though, saying that
Cannot find the type 'Constants.Group1Constants'. Note that type names are case sensitive.
I must be missing something simple. All I want to do is use some constants from a class in different project in my xaml.
Any suggestions?
Solution 1:[1]
For textbox label text field constants in XAML I use:
Text="{x:Static local:[myConstantNamesClass].[name of constant string in your constant class]}"
and then need to build the solution to make it accessible.
(Note that the local: XAML namespace would be set to reference the CLR namespace of the Constants class at the very top of the XAML file.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | phoenixstudio |
