Daniel Wardin
Daniel Wardin

Reputation: 1858

Silverlight OneWay binding

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?

Upvotes: 0

Views: 246

Answers (1)

blindmeis
blindmeis

Reputation: 22445

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

   }

xaml

 <TextBox Text="{Binding MyText, Mode=OneWay}"/>
 <Button Click="button1_Click" />

binding still works no matter if the user change the value manually. but the user will never get the value from view to viewmodel, because its OneWay

Upvotes: 1

Related Questions