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
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.