2014-12-26 45 views
0

我有一個簡單的用戶控件,我正在構建(類似於下面的插圖),它本質上是一個包含多個按鈕的用戶控件。訪問用戶控件中按鈕的Command和CommandParameter屬性

enter image description here

主要控件本身沒有命令或命令參數屬性,但它裏面的四個按鈕做的,我希望能夠訪問那些從視圖模型我碰巧發生的任何意見這個控制。

簡單地說,做這件事的最好方法是什麼。我只是想知道哪個按鈕被點擊。每個按鈕都被命名(所以我會假定照顧身份證方面)。

感謝您的任何建議。

回答

1

如果我正確地理解了你,當你點擊4個按鈕中的一個按鈕時,你想要在viewmodel中調用一個方法。

視圖模型和視圖之間的一般關係的簡化視圖可能有所幫助。從視圖角度來看,通常通過datacontext屬性訪問視圖模型,即datacontext propery包含viewmodel對象。

因此,將xaml中的datacontext或主用戶控件背後的代碼設置爲您要使用的viewmodel對象,並將其從按鈕命令propeties綁定到之前設置的viewmodel上的相應ICommand屬性。

namespace UserControlTest 
{ 
    /// <summary> 
    /// Interaktionslogik für MainUserControl.xaml 
    /// </summary> 
    public partial class MainUserControl : UserControl 
    { 
     public MainUserControl() 
     { 
      InitializeComponent(); 

      DataContext = new ViewModel(); 
     } 
    } 

    public class ViewModel : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     public ViewModel() 
     { 
      Command = new RelayCommand((obj) => Debug.Print("done")); 
     } 

     private ICommand _command; 
     public ICommand Command 
     { 
      get 
      { 
       return _command; 
      } 
      set 
      { 
       _command = value; 

       RaisePropertyChanged("Command"); 
      } 
     } 


     public void RaisePropertyChanged(string propertyName) 
     { 
      if (this.PropertyChanged != null) 
      { 
       this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 

    public class RelayCommand : ICommand 
    { 
     #region Fields 

     readonly Action<object> _execute; 
     readonly Predicate<object> _canExecute; 

     #endregion // Fields 

     #region Constructors 

     public RelayCommand(Action<object> execute) : this(execute, null) { } 

     public RelayCommand(Action<object> execute, Predicate<object> canExecute) 
     { 
      if (execute == null) throw new ArgumentNullException("execute"); 
      _execute = execute; _canExecute = canExecute; 
     } 

     #endregion // Constructors 

     #region ICommand Members 

     public bool CanExecute(object parameter) 
     { 
      return _canExecute == null ? true : _canExecute(parameter); 
     } 

     public event EventHandler CanExecuteChanged 
     { 
      add { CommandManager.RequerySuggested += value; } 
      remove { CommandManager.RequerySuggested -= value; } 
     } 

     public void Execute(object parameter) 
     { 
      _execute(parameter); 
     } 

     #endregion // ICommand Members 
    } 
} 

<UserControl x:Class="UserControlTest.MainUserControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <StackPanel> 
      <Button Height="50" Command="{Binding Command}"></Button> 
     </StackPanel> 
    </Grid> 
</UserControl> 
+0

謝謝你的回答。我理解DataContext和中繼命令的概念。我目前的問題是訪問不直接由用戶控件暴露的按鈕的命令屬性。因此,例如,我應該在用戶控件本身上創建一個命令和命令參數屬性,並將按鈕連接到該按鈕,然後我可以通過我的視圖模型和中繼命令連接到這些按鈕? –

+0

通過datacontext,可以直接從按鈕綁定到viewmodel,並在用戶控件本身上創建一個command屬性。你可以在我提供的代碼末尾的xaml例子中看到。這是可能的,因爲datacontext值是沿着邏輯樹繼承的(爲用戶控件設置它,在按鈕上使用它)。我更喜歡這種方法,因爲在用戶控件上創建命令讓我感覺像複製按鈕上的命令。如果沒有關於你的例子的更詳細的信息,我想我不能再提供幫助。 – user1182735

+0

謝謝,這教會了我一些我沒有意識到你可以做的事情。現在一個簡單的問題。在你的按鈕用戶控件的xaml示例中,如果我想將該按鈕作爲commandParameter傳遞,我應該如何在xaml中聲明它? –

相關問題