2010-08-10 179 views
0

我在窗口中列出了複選框,指定了一些要訂購的項目。當窗口加載並在選擇/檢查某些項目(複選框)後啓用它時,我需要先禁用訂單按鈕,反之亦然。我已經綁定了複選框的IsChecked屬性。在複選框上啓用禁用按鈕選中/取消選中wpf mvvm

編輯導入從OP評論: -

我ItemsControl中的唯一一個複選框。我已將ItemsControl的ItemsSource綁定到列表。這樣我們可以根據列表中的項目顯示多個複選框。

下面是代碼:

<ItemsControl ItemsSource="{Binding FavoriteItems}" Margin="80,0"> 
    <ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <Grid> 
     <StackPanel> 
      <Grid> 
      <CheckBox IsChecked="{Binding IsHouseholdSelected}" Content="{Binding SubCategoryName}" Grid.ColumnSpan="1" FontFamily="Calibri" FontWeight="Bold" /> 
      </Grid> 
     </StackPanel> 
     </Grid> 
    </DataTemplate> 
    </ItemsControl.ItemTemplate> 
+0

你有一些XAML代碼?目前還不清楚你有多少複選框,它們在哪裏以及它們具有什麼樣的功能。一些代碼可以幫助大部分時間... – gehho 2010-08-10 06:26:04

+0

我在ItemsControl中只有一個複選框。我已將ItemsControl的ItemsSource綁定到列表。這樣我們可以根據列表中的項目顯示多個複選框。下面是代碼: <複選框器isChecked = 「{結合IsHouseholdSelected}」含量= 「{結合SubCategoryName}」 Grid.ColumnSpan = 「1」 的FontFamily = 「宋體」 fontWeight設置= 「粗體」/> Tarun 2010-08-10 07:29:41

+0

@Turan:請用問題下的編輯功能在您的問題中包含重要的新信息。 – AnthonyWJones 2010-08-10 07:52:07

回答

0

綁定到該按鈕的命令和執行CanExecute方法來檢查複選框的狀態,並啓用或禁用的按鈕,並使用Execute方法來調用功能你想要的按鈕。

MVVM RelayCommand

CanExecute on MSDN

編輯:這裏是如何實現RelayCommand一些源代碼。 RelayCommand類可以在上面提供的第一個鏈接中找到。我假設你知道如何將DataContext連接到ViewModel實現。

<StackPanel> 
    <CheckBox Name="MyCheckBox" Content="Some CheckBox" 
       IsChecked="{Binding MyCheckBoxChecked}"/> 
    <Button Content="Click me" Command="{Binding MyCommand}"/> 
</StackPanel> 

public class OrderViewModel 
{ 
    private RelayCommand MyRelayCommand; 

    public OrderViewModel() 
    { 
     MyRelayCommand = new RelayCommand(Execute, CanExecute); 
     MyCheckBoxChecked = false; 
    } 

    public RelayCommand MyCommand 
    { 
     get { return MyRelayCommand; } 
    } 

    public bool MyCheckBoxChecked { get; set; } 

    private bool CanExecute(object o) 
    { 
     // Here I'm just checking the property we've bound to but you can put 
     // anything in here that will return a bool, including a check of any/all 
     // of the checkboxes you may need to check 
     return MyCheckBoxChecked; 
    } 

    private void Execute(object o) 
    { 
     Console.WriteLine(@"Executing ..."); 
    } 
} 
+0

我已經綁定了一個命令來執行一些其他功能。我正在使用mvvm燈。 請建議? 謝謝 – Tarun 2010-08-10 05:38:41

1

下面是一個示例代碼,可以幫助你。基本上,這裏的關鍵是我有列表中的項目隱式通知其父視圖模型的Command對象每次IsChecked屬性更改時引發CanExecuteChanged事件。 (另外,我在這裏使用「DelegateCommand」,這與「RelayCommand」一樣)。

的ViewModels:

public class ViewModel : INotifyPropertyChanged 
    { 
     public DelegateCommand MyCommand { get; set; } 

     private ObservableCollection<ItemViewModel> items = new ObservableCollection<ItemViewModel>(); 
     public ObservableCollection<Item> Items 
     { 
      get { return this.items; } 
     } 

     public ViewModel() 
     { 
      this.items.Add(new ItemViewModel(this) { IsChecked = false, Text = "Item 1" }); 
      this.items.Add(new ItemViewModel(this) { IsChecked = false, Text = "Item 2" }); 
      this.items.Add(new ItemViewModel(this) { IsChecked = false, Text = "Item 3" }); 

      this.MyCommand = new DelegateCommand(this.CanExecute, this.Execute); 
     } 

     public void Execute(object parameter) 
     { 
      MessageBox.Show("Executed"); 
     } 

     public bool CanExecute(object parameter) 
     { 
      return (this.items.Count == this.items.Count((x) => x.IsChecked)); 
     } 

     #region INotifyPropertyChanged Members 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void OnPropertyChanged(string propName) 
     { 
      if (this.PropertyChanged != null) 
      { 
       this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
      } 
     } 

     #endregion 
    } 

    public class ItemViewModel 
    { 
     private ViewModel parent; 
     private bool isChecked; 

     public string Text { get; set; } 

     public bool IsChecked 
     { 
      get { return this.isChecked; } 
      set 
      { 
       this.isChecked = value; 

       if (this.parent.MyCommand != null) 
        this.parent.MyCommand.OnCanExecuteChanged(null); 
      } 
     } 

     public Item(ViewModel parent) 
     { 
      this.parent = parent; 
     } 
    } 

查看:

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication2" 
     xmlns:sys="clr-namespace:System;assembly=mscorlib" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.DataContext> 
     <local:ViewModel/> 
    </Window.DataContext> 

    <DockPanel> 
     <Button DockPanel.Dock="Bottom" Command="{Binding MyCommand}">Test</Button> 

     <ItemsControl ItemsSource="{Binding Items}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Text}"/> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </DockPanel> 

</Window> 
相關問題