TextBox中的TextProperty在输入文本后未更新;

时间:2016-05-23 18:56:44

标签: c# wpf xaml data-binding

我在Input.xaml中有这个文本框:

 <TextBox Name="input"
     IsEnabled = "{Binding ElementName = control, Path = InputEnabled}"
     Text = "{Binding Input, ElementName = control, UpdateSourceTrigger = PropertyChanged}"
     />

在Input.xaml.cs中:

public static readonly DependencyProperty InputProperty = DependencyProperty.Register(
    "Input",
    typeof(string),
    typeof(InputPanel),
    new FrameworkPropertyMetadata("")
    );

public static readonly DependencyProperty InputEnabledProperty = DependencyProperty.Register(
    "InputEnabled",
    typeof(bool),
    typeof(InputPanel),
    new FrameworkPropertyMetadata(true)
    );

public string Input
{
    get { return (string)GetValue(InputProperty); }
    set { SetValue(InputProperty, value); }
}

public bool InputEnabled
{
    get { return (bool)GetValue(InputEnabledProperty); }
    set { SetValue(InputEnabledProperty, value); }
}
//...

我从Windows.xaml更新属性Input,如下所示:

   <local:Input Input = "{Binding Path = Selected.ETA, Mode = OneWay}"/>

完美无缺,但是当我从GUI更改Text Property时,绑定再也无法工作了。从GUI进入文本后,有一种方法可以使用绑定。

1 个答案:

答案 0 :(得分:1)

您应该按照以下方式更改binding模式:

  <local:Input Input = "{Binding Path = Selected.ETA, Mode = TwoWay}"/>

这样您就可以从UI更新底层属性并从底层属性更新UI。请记住,您还必须实现INotifyPropertyChange接口。可以找到有关INotifyPropertyChange的更多信息here