2014-11-04 54 views
3

嗯,我正在做一个小项目,我发现没有必要实现一个完整的MVVM。如何在不使用MVVM的情况下绑定DependencyProperty

我想绑定代码背后的一些属性,但无法设法使其工作。

重点是在后面的代码中使用DependencyProperties和Binding。

我试图按照这些链接和问题在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

但它们与MVVM或者至少我无法适应的代码我案件。

该示例应该非常简单。

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(); 
} 

任何想法?

。如果这是一个不好的做法,我应该使用MVVM评论,欢迎使用。

。也......与Command财产有关。在这种情况下,我不想使用MVVM方法,注册事件更好吗?我发现使用自定义命令绑定有点乏味。

+1

其糟糕的做法,你应该使用MVVM(你说我们可以评论;))。 – BradleyDotNET 2014-11-04 19:20:10

回答

2

首先,您可以完全使用没有MVVM的绑定。我不会推荐它,因为当你使用MVVM时代码更加干净,但是可以完成。所有你需要做的就是把这条线放在你的构造函数中:

this.DataContext = this; 

现在你的视图也是你的视图模型!就像我说的,不是一个好主意。

现在,您的代码在您的MainWindow类中有一个DependencyProperty不要那样做。它绝对没有用处。 DP在那里父母控件可以给它们一个绑定。 MainWindow没有父母;所以DP是无用的。

所有你需要做的是建立一个普通的属性:(?我提到它使得使用一个简单的视图模型更有意义)

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

再有MainWindow实施INotifyPropertyChanged

回答你的Command问题。是的,如果您反对使用命令,请注册事件。但是,Command是一种非常好的方式,可以在不中断MVVM的情况下将用户点击到视图模型中。语法不是不好。不过,如果你打算以“视图模型视图”的方式,Command并不会给你带来太多的收益。

+0

谢谢。很容易。是的,最终我想我会使用MVVM方法。我曾经为它们定制过,但是我发现了非常好的框架,比如mvvmlight或者caliburn – blfuentes 2014-11-04 19:25:36

+0

@blacai继续前进并使用你最喜欢的框架,但是我花了五分钟的时间为每个新项目建立自己的框架。它*真的*并不那么难。很高兴听到你会以正确的方式做到这一点! – BradleyDotNET 2014-11-04 19:27:22

+0

那么,目前我没有最喜欢的框架。就像我说的,我用它来创建自己的,但我看到很多关于它们的问题,并且很高兴看一看:) – blfuentes 2014-11-04 19:29:03

相关问题