2013-07-24 49 views
2

我有一個按鈕,並且每次更改選項卡時都想更改點擊處理程序。我希望用Binding來執行此操作。將一個按鈕綁定到所選項目

<Window x:Class="BWCRenameUtility.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:vpan="clr-namespace:BWCRenameUtility.View.VersionPanels" 
     Title="MainWindow" Height="526" Width="525"> 
    <Grid> 
     <DockPanel> 
      <TextBlock Text="Foo" DockPanel.Dock="Top" TextWrapping="Wrap" Padding="10" /> 
      <Grid DockPanel.Dock="Bottom"> 
       <!-- This is not correct, how do I perform this binding correct? --> 
       <Button Content="Export..." HorizontalAlignment="Right" Margin="10" Click="{Binding SelectedItem.Content.PerformExport,ElementName=tabcontrol}" /> 
      </Grid> 
      <TabControl Name="tabcontrol"> 
       <TabItem Header="1.2.5"> 
        <vpan:VersionPanel1_2_5/> 
       </TabItem> 
       <TabItem Header="1.2.8"> 
        <vpan:VersionPanel1_2_8/> <!-- These can be of the same Type by inheritance --> 
       </TabItem> 
      </TabControl> 
     </DockPanel> 
    </Grid> 
</Window> 

正如你所看到的,Button.Click綁定不正確,我想知道這是如何工作在WPF中。

回答

4

您可以Commands做到這一點,你會爲每個玩具TabItemViewModelsICommand並綁定ButtonsCommand屬性設置爲Command

RelayCommand是處理這樣的東西很常見的方式,並可以在整個應用程序中使用

繼電器命令:

public class RelayCommand : ICommand 
    { 
     #region Fields 

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

     #endregion 

     #region Constructors 

     /// <summary> 
     /// Initializes a new instance of the <see cref="RelayCommand"/> class. 
     /// </summary> 
     /// <param name="execute">The execute.</param> 
     public RelayCommand(Action<object> execute) : this(execute, null) { } 

     /// <summary> 
     /// Initializes a new instance of the <see cref="RelayCommand"/> class. 
     /// </summary> 
     /// <param name="execute">The action to execute.</param> 
     /// <param name="canExecute">The can execute.</param> 
     /// <exception cref="System.ArgumentNullException">execute</exception> 
     public RelayCommand(Action<object> execute, Predicate<object> canExecute) 
     { 
      if (execute == null) 
       throw new ArgumentNullException("execute"); 

      _execute = execute; 
      _canExecute = canExecute; 
     } 

     #endregion 

     #region ICommand Members 

     /// <summary> 
     /// Defines the method that determines whether the command can execute in its current state. 
     /// </summary> 
     /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param> 
     /// <returns> 
     /// true if this command can be executed; otherwise, false. 
     /// </returns> 
     [DebuggerStepThrough] 
     public bool CanExecute(object parameter) 
     { 
      return _canExecute == null ? true : _canExecute(parameter); 
     } 

     /// <summary> 
     /// Occurs when changes occur that affect whether or not the command should execute. 
     /// </summary> 
     public event EventHandler CanExecuteChanged 
     { 
      add { CommandManager.RequerySuggested += value; } 
      remove { CommandManager.RequerySuggested -= value; } 
     } 

     /// <summary> 
     /// Defines the method to be called when the command is invoked. 
     /// </summary> 
     /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param> 
     public void Execute(object parameter) 
     { 
      _execute(parameter); 
     } 

     #endregion 
    } 


而且你會在使用在您的應用程序中以下時尚

視圖模型或控制:

public class VersionPanel1_2_8 : VersionPanel 
{ 
    public ICommand MyCommand { get; internal set; } 

    public VersionPanel1_2_8() 
    { 
      MyCommand = new RelayCommand(x => MethodToExecute()); 
    } 

    private void MethodToExecute() 
    { 

    } 
} 

的Xaml:

<Window x:Class="BWCRenameUtility.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:vpan="clr-namespace:BWCRenameUtility.View.VersionPanels" 
     Title="MainWindow" Height="526" Width="525"> 
    <Grid> 
     <DockPanel> 
      <TextBlock Text="Foo" DockPanel.Dock="Top" TextWrapping="Wrap" Padding="10" /> 
      <Grid DockPanel.Dock="Bottom"> 
       <!-- This is not correct, how do I perform this binding correct? --> 
       <Button Content="Export..." HorizontalAlignment="Right" Margin="10" Command="{Binding SelectedItem.Content.MyCommand,ElementName=tabcontrol}" /> 
      </Grid> 
      <TabControl Name="tabcontrol"> 
       <TabItem Header="1.2.5"> 
        <vpan:VersionPanel1_2_5/> 
       </TabItem> 
       <TabItem Header="1.2.8"> 
        <vpan:VersionPanel1_2_8/> <!-- These can be of the same Type by inheritance --> 
       </TabItem> 
      </TabControl> 
     </DockPanel> 
    </Grid> 
</Window> 
3

您需要Button的Command屬性綁定到WPF喜歡你的命令,

Command="{Binding SelectedItem.Content.PerformExport, ElementName=tabcontrol}" 

點擊是一個事件,如果你願意,你也可以做大事命令綁定(綁定任何事件在您的視圖模型命令),但是,是不是在你的情況需要

相關問題