XAML:将文本框最大长度绑定到类常量
我正在尝试将 WPF 文本框的 Maxlength 属性绑定到类深处的已知常量。 我正在使用c#。
该类的结构与以下内容不太相似:
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;
}
}
}
}
}
是的,它是深层嵌套的,但不幸的是,在这种情况下,如果不需要大量重写,我就无法将事物移动太多。
我希望能够将文本框 MaxLength 绑定到 Length1 或 Length2 值,但我无法让它工作。
我期望绑定类似于以下内容:
<Textbox Text="{Binding Path=MyBindingValue}" MaxLength="{Binding Path=Blah.One.Two.MetaData.Sizes.Length1}" />
感谢任何帮助。
非常感谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
期间参考属性。 加号指的是内部类。
Periods reference properties. Plus signs refer to inner classes.
固定的!
最初我尝试这样做:
编译正常,但是在运行时绑定失败,尽管类“Two”是数据上下文,但路径无法解析为内部静态类(我可以这样做吗:{Binding Path={ x:Static MetaData+Size.Length1}} ?)
我不得不稍微调整一下类的布局,但绑定现在可以工作了。
新的类结构:
那么我的绑定语句如下:
并且
看起来工作正常。 我不确定是否需要将常量转换为属性,但这样做似乎没有任何害处。
感谢大家的帮助!
Fixed!
Initially I tried doing this:
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:
Then my binding statement is as follows:
and
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!
尝试与 x:Static 绑定。 将具有 Sizes 命名空间的 xmlns:local 命名空间添加到您的 xaml 标头,然后使用如下所示的内容进行绑定:
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:
不幸的是,在执行以下操作时,我收到错误
Type 'One.Two.MetaData.Sizes' not find
。 我无法创建比“Blah”更深的本地命名空间(无论如何,根据 VS2008)并且
Unfortunately, with the following I get the error
Type 'One.Two.MetaData.Sizes' not found
. I cannot create a local namespace deeper than "Blah" (well according to VS2008 anyway)and
如果 One 不是静态类,则无法使用 x:Static 绑定到它。 为什么使用内部类? 如果元数据不大于 2,并且 Sizes 是一个属性,您可以使用 x:Static 轻松访问它。
在这种情况下,您不能绑定到类型,只能绑定到现有对象。 但“一”和“二”是类型,而不是对象。
if One is not a static class you cannot bind to it with x:Static. why using inner classes? if metadata is outside of two, and Sizes is a property, you can easily access it with x:Static.
you cannot bind to types in this case, only to existing objects. but One and Two are types, not objects.