2013-08-23 53 views
2

我有一個數據視圖綁定到一個名爲Activity的類。在網格中有兩列,分別是複選框列和文本框。在WPF中根據數據視圖值啓用按鈕

我想添加一個代碼,如果至少有一個複選框被選中,並且其中一個文本框有一個特定的字符串,則啓用按鈕。我想過在Activity類中創建一個新的Property,並將它綁定到「isEnabled」屬性,但我不確定這是否會工作,因爲沒有set方法會觸發NotifyPropertyChanged。

有什麼建議嗎?

<DataGrid Name="dataGrid" HorizontalAlignment="Center" CanUserAddRows="False" CanUserReorderColumns="False" HeadersVisibility="Column" Margin="0,28,0,0" VerticalAlignment="Top" Height="262" AutoGenerateColumns="False" ItemsSource="{Binding Activities}" SelectedItem="{Binding SelectedActivity, Mode=TwoWay}"> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn Header="Enabled"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding Enabled, UpdateSourceTrigger=PropertyChanged}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
+0

使用上的按鈕RelayCommand或DelegateCommand並添加在這種情況下,對CanExecute方法的邏輯需要執行。 –

+0

您可以使用中繼命令並檢查CanExecute中所選屬性的值。 –

回答

2

按我的評論上面,你可以利用RelayCommand,因爲它是對自己從而不需要一個NotifyPropertyChanged事件啓用評估了CanExecute函數來執行這樣的動作/禁用Button

下面是一個完整的例子顯示這個工作,因爲它可以是一個有點難以解釋

的XAML:

<Window x:Class="WpfApplication8.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="315" Width="258" Name="UI"> 

    <Grid DataContext="{Binding ElementName=UI}"> 
     <DataGrid Name="dataGrid" CanUserAddRows="False" CanUserReorderColumns="False" HeadersVisibility="Column" AutoGenerateColumns="False" ItemsSource="{Binding Activities}" SelectedItem="{Binding SelectedActivity, Mode=TwoWay}" Margin="10,10,10,37" > 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="Column1" Binding="{Binding Column1}" /> 
       <DataGridTemplateColumn Header="Enabled"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding Enabled, UpdateSourceTrigger=PropertyChanged}"/> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
     </DataGrid> 
     <Button Command="{Binding MyButtonCommand}" Content="Is any checked &amp; is any 'Item6' " Margin="10,0,9,10" Height="22" VerticalAlignment="Bottom"/> 
    </Grid> 
</Window> 

代碼:

namespace WpfApplication8 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     private ObservableCollection<TestObject> myVar = new ObservableCollection<TestObject>(); 

     public MainWindow() 
     { 
      MyButtonCommand = new RelayCommand(ExecuteButtonAction, CanButtonExecute); 
      InitializeComponent(); 

      for (int i = 0; i < 100; i++) 
      { 
       Activities.Add(new TestObject { Column1 = "Item" + i, Enabled = false }); 
      } 
     } 

     public ICommand MyButtonCommand { get; set; } 

     public ObservableCollection<TestObject> Activities 
     { 
      get { return myVar; } 
      set { myVar = value; } 
     } 

     private bool CanButtonExecute() 
     { 
      return Activities.Any(x => x.Enabled) && Activities.Any(x => x.Column1 == "Item2"); 
     } 

     private void ExecuteButtonAction() 
     { 
      // button clicked 
     } 
    } 


    public class TestObject : INotifyPropertyChanged 
    { 

     private string _column1; 
     private bool _enabled; 

     public string Column1 
     { 
      get { return _column1; } 
      set { _column1 = value; NotifyPropertyChanged(); } 
     } 

     public bool Enabled 
     { 
      get { return _enabled; } 
      set { _enabled = value; NotifyPropertyChanged(); } 
     } 


     public event PropertyChangedEventHandler PropertyChanged; 
     public void NotifyPropertyChanged([CallerMemberName]string propertyName = null) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 

    public class RelayCommand : ICommand 
    { 
     private readonly Action _execute; 
     private readonly Func<bool> _canExecute; 

     /// <summary> 
     /// Creates a new command that can always execute. 
     /// </summary> 
     /// <param name="execute">The execution logic.</param> 
     public RelayCommand(Action execute) 
      : this(execute, null) 
     { 
     } 

     /// <summary> 
     /// Creates a new command. 
     /// </summary> 
     /// <param name="execute">The execution logic.</param> 
     /// <param name="canExecute">The execution status logic.</param> 
     public RelayCommand(Action execute, Func<bool> canExecute) 
     { 
      if (execute == null) 
       throw new ArgumentNullException("execute"); 

      _execute = execute; 
      _canExecute = canExecute; 
     } 

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

     public event EventHandler CanExecuteChanged 
     { 
      add 
      { 
       if (_canExecute != null) 
       { 
        CommandManager.RequerySuggested += value; 
       } 
      } 
      remove 
      { 
       if (_canExecute != null) 
       { 
        CommandManager.RequerySuggested -= value; 
       } 
      } 
     } 

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

} 

結果:(是任何項目檢查,併爲任何ITE 「項6」)

enter image description hereenter image description here

相關問題