0

I am trying to understand the MVVM model in Windows Store Application.

The problem is:

  1. I've created an dependency property:

    public string text
    {
        get { return (string)GetValue(textProperty); }
        set { SetValue(textProperty, value); }
    }
    public static readonly DependencyProperty textProperty =
        DependencyProperty.Register("text", typeof(string), typeof(MainPage),  new PropertyMetadata("INIT"));
    
  2. Added it to my DefaultViewModel which is an ObservableDictionary automatically generated via Visual Studio 2013:

    public MainPage()
    {
        this.InitializeComponent();
        this.DefaultViewModel.Add("text", text);
        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += navigationHelper_LoadState;
        this.navigationHelper.SaveState += navigationHelper_SaveState;
    }
    
  3. Next binding to a textBox in XAML file:

    <TextBox Name="textBox" Height="50" Text="{Binding text}"></TextBox>
    

Where DefaultViewModel is binded as datacontext in xaml file:

    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"

Untill this. Everything is perfect. But now i am adding a button:

    <Button Width="500" Height="500" Click="btn_Click">zmien</Button>

    private void btn_Click(object sender, RoutedEventArgs e)
    {
        text = "change it!";
    }

And this is not affecting my text of TextBox when application is running. Can You explain a bit this mechanism for me ? Please.

Grzesiu
  • 9
  • 1
  • 5

2 Answers2

0

You should read the following Tutorial for Windows Store Apps with C# and XAML:

Data binding overview (Windows Store apps using C#/VB/C++ and XAML)

For changes to the source object to propagate to the target, the source must implement the INotifyPropertyChanged interface.

Code sample from the link:

// Create a class that implements INotifyPropertyChanged.
public class DefaultViewModel: INotifyPropertyChanged
{
    private String _text;

    // Declare the PropertyChanged event.
    public event PropertyChangedEventHandler PropertyChanged;

    // Create the property that will be the source of the binding.
    public String Text
    {
        get { return _text; }
        set
        {
            _text = value;
            // Call NotifyPropertyChanged when the source property 
            // is updated.
            NotifyPropertyChanged("Text");
        }
    }

    // NotifyPropertyChanged will fire the PropertyChanged event, 
    // passing the source property that is being updated.
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, 
                new PropertyChangedEventArgs(propertyName));
        }
    }
}

If you now change the value of the Text property of the class DefaultViewModel the Textbox will update.

Florian
  • 1,827
  • 4
  • 30
  • 62
0

Try to enable TwoWay binding:

<TextBox Name="textBox" Height="50" Text="{Binding text, Mode=TwoWay}}" />

If this will not work you can System.Diagnostics.PresentationTraceSources.TraceLevel for binding.

Community
  • 1
  • 1
Artur A
  • 7,115
  • 57
  • 60