How to bind DependencyProperty without using MVVM
Well, I am doing a small project and I found that there is no need to implement full MVVM.
I am trying to bind some properties in the code behind, but cannot get it to work.
The point is that the use of DependencyProperties and Binding in the code is behind.
I tried to follow these links and questions on SO:
Bind binding property in codebehind WPF
How to create a binding in code
Bind binding property defined in Code-Behind via Xaml for property in DataContext UserControl
But they are related to MVVM or at least I cannot adapt the code in my case.
The example should be very simple.
MainWindow.xaml
<Label Name="_lblCurrentPath"
Style="{StaticResource CustomPathLabel}"
ToolTip="{Binding CurrentPath}"
Content="{Binding CurrentPath, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
SetBindings();
}
#region Properties
public static readonly DependencyProperty CurrentPathProperty =
DependencyProperty.Register("CurrentPath", typeof(String), typeof(MainWindow), new PropertyMetadata(String.Empty, OnCurrentPathChanged));
public string CurrentPath
{
get { return (String)GetValue(CurrentPathProperty); }
set { SetValue(CurrentPathProperty, value); }
}
#endregion
#region Bindings
private void SetBindings()
{
// Label CurrentPath binding
Binding _currentPath = new Binding("CurrentPath");
_currentPath.Source = CurrentPath;
this._lblCurrentPath.SetBinding(Label.ContentProperty, _currentPath);
}
#endregion
#region Methods
private void Refresh()
{
MessageBox.Show("Refresh!");
}
private string Search()
{
WinForms.FolderBrowserDialog dialog = new WinForms.FolderBrowserDialog();
WinForms.DialogResult _dResult = dialog.ShowDialog();
switch(_dResult)
{
case WinForms.DialogResult.OK:
CurrentPath = dialog.SelectedPath;
break;
default:
break;
}
return CurrentPath;
}
#endregion
#region Events
private static void OnCurrentPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MainWindow instance = d as MainWindow;
instance.Refresh();
}
public void OpenSearchEclipsePath(object sender, RoutedEventArgs e)
{
CurrentPath = Search();
}
public void RefreshEclipsePath(object sender, RoutedEventArgs e)
{
Refresh();
}
Any idea?
.If this is bad practice and I have to use MVVM comments, please from ours.
... Also ... Associated with a property Command
. In this case, when I don't want to use the MVVM approach, is it better to log events? I found using custom command bindings a little tedious.
source to share
First, you can fully use bindings without MVVM. I wouldn't recommend it as the code is much cleaner when using MVVM, but it can be done. All you have to do is put this line in your constructor:
this.DataContext = this;
Now your gaze is also your viewing model! As I said, this is not a good idea.
Now your code has DependencyProperty
in your class MainWindow
. Don't do this . This is not practical. DP so that parent controls can bind to them. MainWindow
has no parent; therefore DP is useless.
All you have to do is set up a regular property:
public string CurrentPath
{
get { return currentPath; }
set
{
currentPath = value;
NotifyPropertyChanged();
}
}
And then MainWindow
implement INotifyPropertyChanged
(did I mention that it makes sense to use a simple view model?).
To answer your question Command
. Yes, if you are against using commands, just register for the events. However, Command
is a really good way to get user clicks into the view model without breaking MVVM. The syntax isn't so bad. If you go for "View as a View Model" anyway, Command
don't buy you much.
source to share