2014-11-04 52 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

相關問題