0

I have 30 checkbox. If i checked any 6 checkbox, remaining 24 checkbox should be disabled and if i unchecked any one checkbox from 6, all checkbox should be enabled.

<Style TargetType="{x:Type ListBox}">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="4"

          CornerRadius="5" Margin="6"

          >

                        <CheckBox Uid="checkbox1" Name="checkbox1" Checked="CheckBox_Checked" Unchecked="CheckBox_UnChecked"  IsChecked="{Binding ElementName=button,Path=IsChecked,Mode=OneWay}">
                            <Image

            Source="{Binding Path=UriSource}"

            Stretch="Fill"

            Width="100" Height="120"

           />
                        </CheckBox>
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter  Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>

 <ListBox Name="List1"  ItemsSource="{Binding}"   Margin="-50,-8,93,0" RenderTransformOrigin="0.5,0.5" Height="289" VerticalAlignment="Top" >
            <ListBox.RenderTransform>

                <TransformGroup>
                    <ScaleTransform ScaleX="0.975" ScaleY="0.997"/>
                    <SkewTransform AngleY="-8.98" AngleX="9.705"/>
                    <RotateTransform Angle="9.419"/>
                    <TranslateTransform Y="76.889" X="64.258"/>
                </TransformGroup>


            </ListBox.RenderTransform>
        </ListBox>

c#:

if (iList.Count == 6) { List1.IsEnabled = false; } 

it disables whole listbox. ilist contains collection of checked checkbox values.

any method to do this? please help me.

Arya
  • 59
  • 1
  • 11

1 Answers1

0

If you want to disable the checkboxes individually i would suggest binding to their IsEnabled property.You can iterate through your list everytime a listbox is checked or unchecked to see if 6 items are checked. If they are you can just iterate through the list again and set the now binded IsEnabled property to false for every item that isn't already checked. I guess you are working in code behind, but i would recommend doing it with a viewmodel. This would look something like this:

ViewModel

public class TestViewModel
{
    private bool _disabledCheckBoxes;

    public TestViewModel()
    {
        TestList = new ObservableCollection<CheckBoxItem>();
        for (int i = 0; i < 30; i++)
        {
            TestList.Add(new CheckBoxItem(true));
        }
    }

    public ObservableCollection<CheckBoxItem> TestList { get; set; }


    public RelayCommand CheckBoxChanged
    {
        get { return new RelayCommand(OnCheckBoxChanged); }
    }

    private void OnCheckBoxChanged()
    {
        if (_disabledCheckBoxes)
        {
            foreach (var item in TestList)
            {
                item.IsEnabled = true;
            }

            _disabledCheckBoxes = false;
        }
        else
        {
            int i = 0;
            foreach (var item in TestList)
            {
                if (item.IsChecked)
                    i++;
            }

            if (i >= 6)
            {
                foreach (var item in TestList)
                {
                    if (!item.IsChecked)
                        item.IsEnabled = false;
                }
                _disabledCheckBoxes = true;
            }
        }
    }
}

CheckBoxItem

public class CheckBoxItem : ObservableObject
{
    public CheckBoxItem(bool isEnabled)
    {
        IsEnabled = isEnabled;
    }

    private bool _isEnabled;
    public bool IsEnabled
    {
        get
        {
            return _isEnabled;
        }
        set
        {
            _isEnabled = value;
            RaisePropertyChanged("IsEnabled");
        }
    }

    private bool _isChecked;
    public bool IsChecked
    {
        get
        {
            return _isChecked;
        }
        set
        {
            _isChecked = value;
            RaisePropertyChanged("IsChecked");
        }
    }
}

Xaml Code / View

<Window x:Class="WpfAppTests.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfAppTests"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    mc:Ignorable="d"
    xmlns:modelNoMvvmLight="clr-namespace:WpfAppTests"
    xmlns:modelMvvmLight="clr-namespace:WpfAppTests.ViewModel"
    Loaded="Loaded_Window"
    Title="MainWindow" Height="350" Width="525" >
<Window.DataContext>
    <modelMvvmLight:TestViewModel/>
</Window.DataContext>

<StackPanel>
    <ListBox ItemsSource="{Binding TestList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding IsChecked}" IsEnabled="{Binding IsEnabled}" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Checked">
                            <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource  AncestorType=Window}, Path=DataContext.CheckBoxChanged}"/>
                        </i:EventTrigger>
                        <i:EventTrigger EventName="Unchecked">
                            <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource  AncestorType=Window}, Path=DataContext.CheckBoxChanged}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </CheckBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>
</Window>

I bound the Checked and UnChecked event with the command CheckBoxChanged(in the View Model) so every time the state of a checkbox changes, your requirement (checked items == 6) can be verified. I used MvvmLight in my example, but if you consider using the mvvm pattern and don't want to use a framework you can implement ObservableObject and RelayCommand yourself.

Michael Hufnagel
  • 537
  • 1
  • 3
  • 13
  • Error 18 The type 'i:InvokeCommandAction' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. C:\Users\documents\visual studio 2012\Projects\simple\simple\MainWindow.xaml 29 30 simple getting error – Arya Apr 05 '18 at 08:52
  • Right click the references of your project -> Add references -> search for System.Windows.Interactivity and add it. – Michael Hufnagel Apr 05 '18 at 08:59
  • i don't have that references – Arya Apr 05 '18 at 09:11
  • @suma are you using visual studio? – Michael Hufnagel Apr 05 '18 at 09:13
  • @suma Hm i guess you can do it [with NuGet](https://stackoverflow.com/questions/8360209/how-to-add-system-windows-interactivity-to-project). – Michael Hufnagel Apr 05 '18 at 09:19
  • No sir, i tried your code. but Not able to get deserved output – Arya Apr 06 '18 at 03:19
  • Really that's weird. The requirement was that if 6 checkboxes are marked the remaining 24 should be disabled. If one checkbox is unchecked all checkboxes should be enabled again. If i understood your requirement correctly my code should do exactly that. May i ask what's not working or which output you are getting? Maybe i can help. – Michael Hufnagel Apr 06 '18 at 07:25
  • ObservableObject, RelayCommand() getting error for this methods. which name space is used?? – Arya Apr 06 '18 at 08:04
  • @suma I used the mvvmlight framework in my example, which has both these classes. But if you don't want to use a framework u can implement both classes yourself. This [link](http://www.markwithall.com/programming/2013/03/01/worlds-simplest-csharp-wpf-mvvm-example.html) i provided in my answer has an implementation of both classes (RelayCommand is called DelegateCommand in the example). It also explains mvvm a little.^^ – Michael Hufnagel Apr 06 '18 at 08:22