I am trying to understand the MVVM model in Windows Store Application.
The problem is:
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"));
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; }
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.