【问题标题】:Binding static class property to textbox将静态类属性绑定到文本框
【发布时间】:2012-09-13 20:27:04
【问题描述】:

我想将textbox 绑定到静态类的属性。我希望这是双向绑定。我的静态类是这样的(修剪):

public static class ocrVar
{
    static ocrVar()
    {  
        MeterNumber = new Element();
    }
}

Element 类看起来像这样(修剪):

public class Element
    {

        public List<string> Value { get; set; }

        public Element()
            : this(new List<string>())
        {
        }
        public Element(List<string> value)
        {
        Value = value;
        }
    }

如果我想获取TextBox 并将其绑定到 ocrVar.MeterNumber.Value[0],有没有办法做到这一点?

【问题讨论】:

标签: c# wpf data-binding static


【解决方案1】:

由于它是 static 类,并且您想要执行 双向绑定,因此您已经提供了路径并提供了一个非静态类作为技巧并使用绑定

在你的情况下,会是

<Window.Resources>
    <local:ocrVar x:Key="ocrVarManager"/>
</Window.Resources>

<TextBox Text="{Binding Source={StaticResource ocrVarManager}, Path=MeterNumber.Value[0]}"/>

可以参考Binding to Static Property

【讨论】:

  • 当我尝试这个时,我得到“找不到类型 'local:ocrVar。”
猜你喜欢
  • 1970-01-01
  • 2014-09-10
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 2011-04-21
  • 2016-01-06
相关资源
最近更新 更多