Могу ли я связать WPF TextBox или другой элемент управления со свойством IronPython? - PullRequest
0 голосов
/ 06 октября 2019

Я реализую UserControl в IronPython. Я хочу привязать элемент управления к свойству 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')

.. и когда вводится текст, вместо вызова метода установки он перезаписывает объект свойства новым текстом. Есть ли способ сделать эту работу? Если нет, то какая лучшая альтернатива?

Вот мой код:

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

1 Ответ

0 голосов
/ 06 октября 2019

xclass должен наследоваться от System.Object!

class xclass(System.Object):

Вот и все.

...