2010-08-10 175 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> 
相关问题