I started learning WPF few days ago and have been creating some test projects to evaluate my understanding of the materials as I learn them. According to this article, "a property can only be bound if it is a dependency property." To test this, I used two different patterns:
1. Using MVVM pattern (sort of)
I created the Movie
model:
public class MovieModel
{
private string _movieTitle;
private string _rating;
public string MovieTitle
{
get { return _movieTitle; }
set { _movieTitle = value; }
}
public string Rating
{
get { return _rating; }
set { _rating = value; }
}
}
and the MovieViewModel
view model:
public class MovieViewModel
{
MovieModel _movie;
public MovieViewModel()
{
_movie = new MovieModel { MovieTitle = "Inception (Default)" };
}
public MovieModel Movie
{
get { return _movie; }
set { _movie = value; }
}
public string MovieTitle
{
get { return Movie.MovieTitle; }
set { Movie.MovieTitle = value; }
}
}
And finally in the my main View
, I set the DataContext to an instance of the MovieViewModel
class:
<Window x:Class="MVVMTestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMTestProject.ViewModels"
Name="MainWindowElement"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MovieViewModel/>
</Window.DataContext>
<Grid>
<StackPanel Orientation="Vertical">
<TextBox Width="150" Text="{Binding Path=MovieTitle}"></TextBox>
</StackPanel>
</Grid>
This works fine, when I run the application I see Inception (Default)
in the textbox, even though none of the properties in the Model
or ViewModel
are DependencyProperties
. Now the second try was by:
2. Using a property in the code behind of MainView
In the code behind of my main view, I put:
private string _myText;
public MainWindow()
{
InitializeComponent();
MyText = "Some Text";
}
public string MyText
{
get { return _myText; }
set { set _myText = value; }
}
And then I changed my view to:
<Window x:Class="MVVMTestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMTestProject.ViewModels"
Name="MainWindowElement"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MovieViewModel/>
</Window.DataContext>
<Grid>
<StackPanel Orientation="Vertical">
<TextBox Width="150" Text="{Binding ElementName=MainWindowElement, Path=MyText}"></TextBox>
</StackPanel>
</Grid>
But that didn't work. When I run the application, the textbox is blank. So I changed the property to a dependency property, and oddly enough, it worked. So I don't understand why the properties on the Model and ViewModel can be regular properties, but the code-behind properties have to be dependency properties in order to bind to? Is it because the ViewModel is set to the DataContext of the view and the parser is smart enough to realize this or is there another reason?
Also a not-very-relevant question: I noticed in almost every singe article on WPF people use a local variable and then define a getter and setter for it, even though we all know in .NET 2.5 (I believe?) and above, we can just use the get; set;
syntax without having to define a local member variable. So is there a specific reason for this?
Thanks in advance.