【问题标题】:enabling disabling button on checkbox check/uncheck wpf mvvm启用复选框选中/取消选中 wpf mvvm 上的禁用按钮
【发布时间】:2010-08-10 05:27:57
【问题描述】:

我在一个窗口上有一个复选框列表,指定了一些要订购的项目。我需要在加载窗口时首先禁用 Order 按钮,并在选择/检查某些项目(复选框)后启用它,反之亦然。我已经绑定了复选框的 IsChecked 属性。

编辑从 OP 评论导入:-

我在 ItemsControl 中只有一个复选框。我已将 ItemsControl 的 ItemsSource 绑定到 List。这样我们就可以根据列表中的项目显示多个复选框。

代码如下:

<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>

【问题讨论】:

  • 你有一些 XAML 代码吗?!目前尚不清楚您有多少个复选框,它们在哪里,以及它们具有什么样的功能。一些代码在大多数情况下都有帮助......
  • 我在 ItemsControl 中只有一个复选框。我已将 ItemsControl 的 ItemsSource 绑定到 List。这样我们就可以根据列表中的项目显示多个复选框。这是代码:
  • @Turan:请使用问题下方的编辑功能在您的问题中包含重要的新信息。
  • 会处理的。谢谢

标签: wpf silverlight mvvm


【解决方案1】:

以下是可以帮助您的示例代码。基本上,这里的关键是我让列表中的项目隐式通知其父 ViewModel 的 Command 对象以在每次 IsChecked 属性更改时引发 CanExecuteChanged 事件。 (另外,我这里用的是“DelegateCommand”,和“RelayCommand”一样)。

视图模型:

    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>

【讨论】:

    【解决方案2】:

    将命令绑定到按钮并实现 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 ...");
        }
    }
    

    【讨论】:

    • 我已经将命令绑定到按钮以执行其他一些功能。我正在使用 mvvm 灯。请建议?谢谢
    猜你喜欢
    • 2012-04-18
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 2015-12-19
    • 2018-11-07
    • 2011-06-02
    • 2015-12-09
    • 1970-01-01
    相关资源
    最近更新 更多