Let's assume I have a text box on the form and it is enabled (user can type whatever they want.) There is a OneWay binding set up to a string property so when the ViewModel changes the property the text box updates. Now what happens when the user changes the value in the text box manually. Does the binding get overwritten by the new value of the text box? Or does it just remember the value and keep the binding, so when I update the property next time in the ViewModel the change will reflect in the UI?
问问题
240 次
1 回答
1
public class VM : INPCBase
{
private string _myText;
public string MyText
{
get { return _myText; }
set { _myText = value; this.NotifyPropertyChanged(()=>MyText);}
}
public void Blup()
{
this.MyText = "blup";
}
}
public partial class MainWindow : Window
{
private VM data = new VM();
public MainWindow()
{
InitializeComponent();
data.MyText = "sdfjksj";
this.DataContext = data;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.data.Blup();
}
}
xml
<TextBox Text="{Binding MyText, Mode=OneWay}"/>
<Button Click="button1_Click" />
无论用户是否手动更改值,绑定仍然有效。但是用户永远不会从视图到视图模型中获得价值,因为它的 OneWay
于 2013-07-01T13:31:40.310 回答