更新绑定而不实现INotifyPropertyChanged?

问题描述

是否可以在不实现INotifyPropertyChanged接口的情况下更新文本框, 例如触发或事件来自我的viewmodel?

我的示例代码

UserControl:

<StackPanel VerticalAlignment="Center">
    <TextBox Text="{Binding Model.ExampleClass.MyProperty,Mode=TwoWay}" Height="20" Width="400"/>
    <TextBox Text="{Binding Model.TestProperty,Mode=TwoWay}" Height="20" Width="400"/>
</StackPanel>
    <Button Command="{Binding ButtonClickCommand}" Grid.Row="1" Content="Click" Height="40"/>

viewmodel:

private Model _model = new Model();

public Model Model
{
    get { return _model; }
    set { SetProperty(ref _model,value); }
}

public ICommand ButtonClickCommand { get; private set; }

public viewmodel()
{
    ButtonClickCommand = new RelayCommand(ButtonClickExecute,ButtonClickCanExecute);
}

#region ICommand
private bool ButtonClickCanExecute(object obj)
{
    return true;
}

private void ButtonClickExecute(object obj)
{
    Model.ExampleClass.MyProperty = 50;
    Model.TestProperty = 50;
}
#endregion

型号:

private ExampleClass _exampleClass = new ExampleClass() { MyProperty = 30};
private int _testProperty = 30;

public ExampleClass ExampleClass
{
    get { return _exampleClass; }
    set { SetProperty(ref _exampleClass,value); }
}

public int TestProperty
{
    get { return _testProperty; }
    set { SetProperty(ref _testProperty,value); }
}

ExampleClass(无法更改):

public class ExampleClass
{
    public int MyProperty { get; set; }
}

我的问题

我想通过单击按钮来更新MyProperty中的属性ExampleClass(不执行INotifyPropertyChanged或需要对该类进行任何其他更改)。

解决方法

您可以尝试获取TextBox.TextPropery的绑定表达式并手动触发更新。例: textBox1.GetBindingExpression(TextBox.TextProperty).UpdateTarget();

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...