3

Well, I am doing a small project and I found it wasn't necessary to implemente a full MVVM.

I am trying to bind some properties in code behind, but cannot manage to make it works.

The point is the use of DependencyProperties and Binding in code behind.

I tried to follow these links and questions in SO:

Bind Dependency Property in codebehind WPF

How to: Create a Binding in Code

Bind Dependency Property, defined in Code-Behind, through Xaml to a Property in the DataContext of a 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 a bad practice and I should use MVVM comments are welcome, of ourse.

.Also... Related to Command property. In this case where I don't want to use a MVVM approach, is it better to register events? I found the use of custom command bindings a little bit tedious.

1
  • 1
    Its bad practice, you should use MVVM (you said we could comment ;) ). Commented Nov 4, 2014 at 19:20

1 Answer 1

3

First, you can totally use bindings without MVVM. I wouldn't reccommend it, as the code is a lot cleaner when you use MVVM, but it can be done. All you need to do is put this line in your constructor:

this.DataContext = this; 

Now your view is also your view model! Like I said, not a good idea.

Now, the code you have has a DependencyProperty in your MainWindow class. Don't do that. It serves absolutely no purpose. DPs are there so parent controls can give a binding to them. MainWindow has no parent; so a DP is useless.

All you need to do is set up a regular property:

public string CurrentPath { get { return currentPath; } set { currentPath = value; NotifyPropertyChanged(); } } 

And then have MainWindow implement INotifyPropertyChanged (did I mention that it makes more sense to use a simple view model?).

To answer your Command question. Yes, if you are opposed to using commands, just register for the events. However, Command is a really nice way to get user clicks into the view model without breaking MVVM. The syntax isn't that bad. If you are going the "View as a View Model" approach anyways though, Command doesn't buy you much.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. It was easy. Yes, in the end I think I will use a MVVM approach. I used to create them custom, but I found very nice frameworks like mvvmlight or caliburn
@blacai Go right ahead and use your favorite framework, but it takes me all of five minutes to set up my own for each new project I do. It really isn't that hard. Glad to hear you are going to do it the right way though!
Well, at the moment I don't have favorite framework. Like I said I use to create my own but I see a lot of questions about them and would be nice to take a look :)
@blacai Meh, 98% of the time frameworks make my life harder, so I try and avoid them unless I really need them. Good luck in your search though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.