2014-04-02 101 views
2

我有一個選項卡控件包含兩個選項卡,其中一個顯示正在運行的進程的消息,另一個顯示我的網頁!在一個按鈕中綁定兩個命令WPF/MVVM

我有三個按鈕(開始,停止和清除) 當我按開始進程運行和消息顯示。

我需要的是我按下開始按鈕,這兩個選項卡都顯示我所需的內容。這意味着我需要兩個綁定兩個命令到同一個按鈕

你知道如何綁定1按鈕的兩個命令?

我使用的是MVVM,而我是MVVM中的新手。

<Button Content="Start" Command="{Binding StartProcess_Command }" Height="25" Width="60" Grid.Row="0" Grid.Column="0" Margin="10"/> 
<Button Content="Stop" Command="{Binding StopProcess_Command}" Height="25" Width="60" Grid.Row="0" Grid.Column="1" Margin="10"/> 
<Button Content="Clear" Command="{Binding ClearBtn_Command}" Height="25" Width="60" Grid.Row="0" Grid.Column="2" Margin="10"/> 
<telerik:RadTabControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Grid.ColumnSpan="4" Margin="10"> 
    <telerik:RadTabItem Header="Maestro" IsSelected="{Binding Path=MaestroSelected}"> 
     <ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible"> 
      <local:WPFBrowser/> 
     </ScrollViewer> 
    </telerik:RadTabItem> 
    <telerik:RadTabItem Header="Messages"> 
     <Grid Background="LightGray"> 
      <ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible"> 
       <TextBlock TextWrapping="Wrap" Text="{Binding Path=output_process,UpdateSourceTrigger=Default}"/> 
      </ScrollViewer> 
     </Grid> 
    </telerik:RadTabItem> 
</telerik:RadTabControl > 

這是WPFBrowser.cs代碼:

  public WPFBrowser() 
      { 
       ServerString = "localhost"; //defualt value 
       PortString = "9999"; //default value 
       this.refreshCommand = new Lunch_Maestro.ViewModel.Command(DoRefreshCommand); 
       m_WPFBrowser = new WebBrowser() 
       { 
        HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, 
        VerticalAlignment = System.Windows.VerticalAlignment.Stretch 
       }; 

       m_WPFBrowser.Navigated += WPFBrowser_Navigated; 

       this.Content = m_WPFBrowser; 
      } 

      private void WPFBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) 
      { 
       this.HideScriptErrors(m_WPFBrowser, true); 
      } 


      /// <summary> 
      /// Found this link with interesting scenerios for script erros... 
      /// http://social.msdn.microsoft.com/Forums/vstudio/en-US/4f686de1-8884-4a8d-8ec5-ae4eff8ce6db/new-wpf-webbrowser-how-do-i-suppress-script-errors?forum=wpf 
      /// </summary> 
      public void HideScriptErrors(WebBrowser wb, bool Hide) 
      { 
       FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic); 
       if (fiComWebBrowser == null) return; 

       object objComWebBrowser = fiComWebBrowser.GetValue(wb); 

       if (objComWebBrowser == null) return; 

       objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide }); 

      } 


      private void DoRefreshCommand() 
      { 
       m_WPFBrowser.Navigate(new Uri("http://" + ServerString + ":" + PortString + "/")); 
      } 

      private bool _maestroSelected; 
      public bool MaestroSelected 
      { 
       get { return _maestroSelected; } 
       set 
       { 
        _maestroSelected = value; 
        if (_maestroSelected == true) 
         m_WPFBrowser.Navigate(new Uri("http://" + ServerString + ":" + PortString + "/")); 
        OnPropertyChanged("MaestroSelected"); 
       } 
      } 

      private readonly WebBrowser m_WPFBrowser; 

      private string _serverString; 
      public string ServerString 
      { 
       get { return _serverString; } 
       set 
       { 
        _serverString = value; 
        OnPropertyChanged("ServerString"); 
       } 
      } 

      private string _portString; 
      public string PortString 
      { 
       get { return _portString; } 
       set 
       { 
        _portString = value; 
        OnPropertyChanged("PortString"); 
       } 
      } 

      private Lunch_Maestro.ViewModel.Command refreshCommand; 
      public Lunch_Maestro.ViewModel.Command RefreshCommand 
      { 
       get { return refreshCommand; } 
      } 
// and Inotify property changes here 

回答

3

你可以從Prism框架使用CompositeCommand。 在viewmodel中創建一個額外的CompositeCommand,使用此CompositeCommand註冊兩個普通命令並將該按鈕綁定到CompositeCommand。

請參閱第130頁的Prism documentation瞭解如何使用CompositeCommands。

您的應用需要引用Prism包中包含的Microsoft.Practices.Prism.dll。

4

你想單擊一個Button並運行兩段代碼......它聽起來並不複雜。利用現有RelayCommands之一,這個問題可以被固定爲簡單,因爲這:

public ICommand SomeCommand 
{ 
    get { return new ActionCommand(action) => { RunFirstFunction(action); 
     RunSecondFunction(action) }, canExecute => someCondition); } 
} 

在XAML:

<Button Command="{Binding SomeCommand}" CommandParameter="{Binding SomeObject}"> 
    Click Me 
</Button> 

ActionCommand是我自己的RelayCommand的形式,但所有的delegate ICommand實現的是大致相等,可以這樣工作。 CommandParameter值作爲action變量進入代碼並傳遞給兩個函數。