Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Enabling/Disabing a checkbox (based on its property) on an ICommand execute + WPF + MVVM

$
0
0

Hi,

I have a user control with a list of checkboxes. I have created an observable collection(hard-coded values for testing) to bind the values to the checkboxes. In addition to the caption, I have two boolean properties for the checkboxes based on which I need to enable/disable these checkboxes, when I click on a button. I use a data trigger on the checkbox to accomplish this. However, I am not able to get that to work. Please suggest a solution.

My View:

<UserControl ...... 
             >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="40*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <dxdo:DockLayoutManager Name="dockManager1" dxcore:ThemeManager.ThemeName="Office2007Blue">
        <dxdo:LayoutGroup x:Name="rootGroup" Orientation="Horizontal">
            <dxdo:DocumentGroup x:Name="documentGroup1">
                <dxdo:DocumentPanel x:Name="paneRightPane" Caption="View Pane">
                    <Grid Name="grdActionsPane">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30*"/>
                            <RowDefinition Height="30*"/>
                            <RowDefinition Height="30*"/>
                        </Grid.RowDefinitions>
                        <GroupBox Header="Actions" Grid.Row="1">
                            <StackPanel Orientation="Horizontal" >
                                <Button Command="{Binding DisableNonIMDSChannelsCommand}" Content="Disable checkbox" Height="40"  Margin="4"/>
                              </StackPanel>
                        </GroupBox>
                    </Grid>
                </dxdo:DocumentPanel>
            </dxdo:DocumentGroup>
        </dxdo:LayoutGroup>
        <dxdo:DockLayoutManager.AutoHideGroups>
            <dxdo:AutoHideGroup DockType="Left">
                <dxdo:LayoutPanel x:Name="paneLeftPane" Caption="Navigable Pane">
                    <GroupBox Header="Channel Selection" Margin="4" >
                        <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="80*"/>
                                    <RowDefinition Height="20*"/>
                                </Grid.RowDefinitions>

                                    <ListView VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" x:Name="listView1" ItemsSource="{Binding AllChannels}">
                                    <ListView.ItemsPanel>
                                        <ItemsPanelTemplate>
                                           <UniformGrid Columns="2" />
                                        </ItemsPanelTemplate>
                                    </ListView.ItemsPanel>
                                    <ListView.Resources>
                                        <Style TargetType="{x:Type ListView}">
                                           <Setter Property="ItemContainerStyle">
                                               <Setter.Value>
                                                   <Style TargetType="ListViewItem">
                                                       <Setter Property="VerticalContentAlignment" Value="Top"/>
                                                   </Style>
                                               </Setter.Value>
                                           </Setter>
                                        </Style>
                                    </ListView.Resources>
                                    <ListView.ItemTemplate>
                                    <DataTemplate>
                                           <Grid Name="grdChannels" Margin="4">
                                               <CheckBox Height="20" Content="{Binding Path=ChannelNumber}" HorizontalAlignment="Center" Margin="2" ToolTip="{Binding Path=ChannelFrequency}"
                                                          IsChecked="{Binding Path=IsChecked,Mode=OneWay,ElementName=chkCheckUncheckAll}">
                                                   <CheckBox.Style>
                                                       <Style TargetType="{x:Type CheckBox}">
                                                           <Style.Triggers>
                                                               <DataTrigger Binding="{Binding Path=ChannelListViewModel.IsNonIMDSChannel, Mode=TwoWay}" Value="True">
                                                                   <Setter Property="IsEnabled" Value="False"/>
                                                               </DataTrigger>
</Style.Triggers>
                                                       </Style>
                                                   </CheckBox.Style>
                                               </CheckBox>
                                           </Grid>
                                        </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                                <Border CornerRadius="2" Grid.Row="1">
                                    <CheckBox Name="chkCheckUncheckAll" Height="20" Content="Check/Uncheck all" Margin="4" HorizontalAlignment="Center"/>
                                </Border>
                            </Grid>
                         </GroupBox>
                </dxdo:LayoutPanel>
                </dxdo:AutoHideGroup>
        </dxdo:DockLayoutManager.AutoHideGroups>
    </dxdo:DockLayoutManager>
    </Grid>
</UserControl>

My ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChannelSelection_DockableWindow.DataAccess;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace ChannelSelection_DockableWindow.ViewModel
{
    public class ChannelListViewModel : ViewModelBase
    {
        readonly ChannelRepository _channelRepository;
        public ObservableCollection<Model.Channel> AllChannels
        {
            get;
            private set;
        }
        private bool isNonIMDSChannel;
        public bool IsNonIMDSChannel
        {
            get
            {
                return isNonIMDSChannel;
            }

            set
            {
                isNonIMDSChannel = value;
                OnPropertyChanged("IsNonIMDSChannel");
            }
        }
     
        //Parameterized Constructor
        public ChannelListViewModel(ChannelRepository channelRepository)
        {
            if (channelRepository == null)
            {
                throw new ArgumentNullException("channelRepository");
            }
            _channelRepository = channelRepository;
            this.AllChannels = new ObservableCollection<Model.Channel>(_channelRepository.GetChannels());
        }

        //Commands
        RelayCommand _disableNonIMDSChannelsCommand;
        public ICommand DisableNonIMDSChannelsCommand
        {
            get
            {
                if (_disableNonIMDSChannelsCommand == null)
                {
                    _disableNonIMDSChannelsCommand = new RelayCommand(param => this.DisableNonIMDSChannelsCommandExecute(), param => this.DisableNonIMDSChannelsCommandCanExecute);
                }
                return _disableNonIMDSChannelsCommand;
            }
        }

        //Action and Predicate implementations

        void DisableNonIMDSChannelsCommandExecute()
        {

            foreach (Model.Channel ch in AllChannels)
            {
                if (ch.IsIMDSActivated == false)
                {
                    IsNonIMDSChannel = true;
                   
                }
            }
           
        }

        bool DisableNonIMDSChannelsCommandCanExecute
        {
            get
            {
                if (AllChannels.Count == 0)
                {
                    return false;
                }
                return true;
            }
        }

        //Dispose
        protected override void OnDispose()
        {
            this.AllChannels.Clear();
        }
    }
}

My Entity (Channel) has the following properties:

        public string ChannelNumber { get; set; }
        public string ChannelFrequency { get; set; }
        public bool IsIMDSActivated { get; set; }

My observable collection will have items added like so:

  _channels.Add(Channel.CreateChannel(100, 5500, false));
  _channels.Add(Channel.CreateChannel(104, 5520, true));

So, ideally, when I click on my button, I should have the channel 100 as disabled.

Thanks in advance,

RJ.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>