Search code examples
c#wpfxamlclass-constants

Accessing nested static const


I'm trying to use a nested constant within WPF, but XAML doesn't seem to handle nested static classes.

namespace MyCommon.Constants
{
    public static class Constants
    {
        public static class Formatting
        {
            public static class DateTime
            {
                public const string BritishDateToString = "dd-MM-yy";
            }
        }
    }
}

Import the namespace

xmlns:constants="clr-namespace:MyCommon.Constants;assembly=MyCommon"

The following lines gives an error

<DataGridTextColumn Binding="{Binding Path=Date, StringFormat={x:Static constants:Constants.Formatting.DateTime.BritishDateTimeToString}}" Header="Date" />

Solution

  • Use + for accessing nested classes

    <DataGridTextColumn Binding="{Binding Path=Date, StringFormat={x:Static constants:Constants+Formatting+DateTime.BritishDateTimeToString}}" Header="Date" />