Alex Rivera | Logout

XAML : Binding textbox maxlength to Class constant

Asked 2008-10-24T10:11:40.847
19

I am attempting to bind a WPF textbox's Maxlength property to a known constant deep within a class. I am using c#.

The class has a structure not too dissimilar to the following:

namespace Blah
{
    public partial class One
    {
        public partial class Two
        {
             public string MyBindingValue { get; set; }

             public static class MetaData
             {
                 public static class Sizes
                 {
                     public const int Length1 = 10;
                     public const int Length2 = 20;
                 }
             }
        }
    }
}

Yes it is deeply nested, but unfortunately in this instance I can't move things round very much without huge rewrites required.

I was hoping I'd be able to bind the textbox MaxLength to the Length1 or Length2 values but I can't get it to work.

I was expecting the binding to be something like the following:

<Textbox Text="{Binding Path=MyBindingValue}" MaxLength="{Binding Path=Blah.One.Two.MetaData.Sizes.Length1}" />

Any help is appreciated.

Many thanks

Edit
Report

1 Answer

0

try to bind with x:Static. add a xmlns:local namespace with the namespace of Sizes to your xaml header and then bind with something like this:

{x:Static local:Sizes.Length1}
answered 2008-10-24T10:17:50.633

Your Answer