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
MaxLength="{x:Static local:One+Two+MetaData+Sizes.Length1}"
Periods reference properties. Plus signs refer to inner classes.
Fixed!
Initially I tried doing this:
{Binding Path=MetaData+Sizes.Length1}
which compiled ok, however the binding failed at runtime, despite the Class 'Two' being the datacontext the path couldn't resolve into the inner static classes (could I have done something like : {Binding Path={x:Static MetaData+Size.Length1}} ?)
I had to fiddle with the layout of my classes a little but the binding is now working.
New class structure:
namespace Blah
{
public static class One
{
// This metadata class is moved outside of class 'Two', but in this instance
// this doesn't matter as it relates to class 'One' more specifically than class 'Two'
public static class MetaData
{
public static class Sizes
{
public static int Length1 { get { return 10; } }
public static int Length2 { get { return 20; } }
}
}
public partial class Two
{
public string MyBindingValue { get; set; }
}
}
}
Then my binding statement is as follows:
xmlns:local="clr-namespace:Blah"
and
MaxLength="{x:Static local:MetaData+Sizes.Length1}"
Which appears to work ok. I'm not sure whether or not the constants needed to be converted into properties, but there doesn't appear to be any harm in doing so.
Thankyou everyone for your help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With