【问题标题】:Can I bind a WPF TextBox or other control to an IronPython property?我可以将 WPF 文本框或其他控件绑定到 IronPython 属性吗?
【发布时间】:2019-10-05 21:21:40
【问题描述】:

我正在 IronPython 中实现一个 UserControl。我想将控件绑定到 IronPython 属性。但是当绑定创建时,它会抱怨:

Cannot create default converter to perform 'two-way' conversions between types 'IronPython.Runtime.PythonProperty' and 'System.String'. Consider using Converter property of Binding. BindingExpression:Path=z; DataItem='OldInstance' (HashCode=27321283); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')

.. 当输入文本时,它不会调用 setter,而是用新文本覆盖属性对象。有没有办法使这项工作?如果没有,最好的选择是什么?

这是我的代码:

import System
from System import Action
from System.Windows import Window
from System.Windows.Data import Binding, BindingMode
from System.Windows.Controls import StackPanel, Label, TextBox, Orientation
class xclass():
  def __init__(self):
    self.__y = "ytext"
    self.zval = "ztext"
  @property
  def y(self):
    print "get_y", self.__y
    return self.__y
  @y.setter
  def y(self, value):
    self.__y = value
    print "set_y", value
  def get_z(self):
    print "get_z", self.zval
    return self.zval
  def set_z(self, value):
    print "set_z", value
    self.zval = value
  z = property(get_z, set_z)
x = xclass()
def f():
  win = Window()
  sp = StackPanel()
  sp.Orientation = Orientation.Horizontal
  sp.Height=24
  lb = Label()
  lb.Content="Label0"
  bd1 = Binding("y")
  bd1.Source = x
  bd2 = Binding("z")
  bd2.Source = x
  tb1 = TextBox()
  tb1.SetBinding(TextBox.TextProperty, bd1)
  tb2 = TextBox()
  tb2.SetBinding(TextBox.TextProperty, bd2)
  win.AddChild(sp)
  sp.AddChild(lb)
  sp.AddChild(tb1)
  sp.AddChild(tb2)
  win.Show()
  win.Width = 400
  win.Height = 200
System.Windows.Application.Current.Dispatcher.BeginInvoke(Action(f));

【问题讨论】:

    标签: .net wpf mvvm ironpython


    【解决方案1】:

    xclass 必须继承自 System.Object!

    class xclass(System.Object):
    

    仅此而已。

    【讨论】:

      猜你喜欢
      • 2012-07-23
      • 1970-01-01
      • 2016-02-12
      • 2017-04-17
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      相关资源
      最近更新 更多