XAML:将文本框最大长度绑定到类常量

发布于 2024-07-08 12:38:07 字数 855 浏览 8 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

慕巷 2024-07-15 12:38:07
MaxLength="{x:Static local:One+Two+MetaData+Sizes.Length1}"

期间参考属性。 加号指的是内部类。

MaxLength="{x:Static local:One+Two+MetaData+Sizes.Length1}"

Periods reference properties. Plus signs refer to inner classes.

胡渣熟男 2024-07-15 12:38:07

固定的!

最初我尝试这样做:

{Binding Path=MetaData+Sizes.Length1}

编译正常,但是在运行时绑定失败,尽管类“Two”是数据上下文,但路径无法解析为内部静态类(我可以这样做吗:{Binding Path={ x:Static MetaData+Size.Length1}} ?)

我不得不稍微调整一下类的布局,但绑定现在可以工作了。

新的类结构:

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; }
        }
    }
}

那么我的绑定语句如下:

xmlns:local="clr-namespace:Blah"

并且

MaxLength="{x:Static local:MetaData+Sizes.Length1}"

看起来工作正常。 我不确定是否需要将常量转换为属性,但这样做似乎没有任何害处。

感谢大家的帮助!

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!

叹倦 2024-07-15 12:38:07

尝试与 x:Static 绑定。 将具有 Sizes 命名空间的 xmlns:local 命名空间添加到您的 xaml 标头,然后使用如下所示的内容进行绑定:

{x:Static local:Sizes.Length1}

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}
野侃 2024-07-15 12:38:07

不幸的是,在执行以下操作时,我收到错误 Type 'One.Two.MetaData.Sizes' not find。 我无法创建比“Blah”更深的本地命名空间(无论如何,根据 VS2008)

xmlns:local="clr-namespace:Blah"

并且

MaxLength="{x:Static local:One.Two.MetaData.Sizes.Length1}"

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)

xmlns:local="clr-namespace:Blah"

and

MaxLength="{x:Static local:One.Two.MetaData.Sizes.Length1}"
幽蝶幻影 2024-07-15 12:38:07

如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文