Question Disable buttons if all checkboxes are unticked

sbondo1234

Member
Joined
Jan 21, 2020
Messages
8
Programming Experience
1-3
I have a DataTemplate that holds:
XML:
<DataTemplate x:Key="hostListItem">
    <StackPanel Orientation="Horizontal">
        <CheckBox x:Name="hostCheckBox" Margin="0 0 5 0"
                  Unchecked="hostCheckBox_Unchecked"
                  Checked="hostCheckBox_Checked">
        </CheckBox>
        <TextBlock Text="{Binding Path=Name}"></TextBlock>
    </StackPanel>
</DataTemplate>

I add the template to a ListBox like:
C#:
public class Host
{
    public string Name
    {
        get;
        set;
    }

    public string Address
    {
        get;
        set;
    }
}

C#:
public partial class MainWindow : Window
{
    private void addRule()
    {
        ObservableCollection<Host> hostData = new ObservableCollection<Host>();

        // Add data to Host structure
        hostData.Add(new Host()
        {
            Name = host,
            Address = address
        });

        // Add new host to listbox
        hostsListBox.Items.Add(hostData);
    }
}

The addRule function adds a template to the ListBox, which works.

I have an Unchecked event on the checkbox which I want to use to check if all the other checkboxes are unchecked. If every checkbox is unchecked then it should disable buttons I have.

I can't figure out how to loop over all the CheckBoxes in the ListBox and check if they are ticked or not. How would I do this?
 
Next time provide your whole XAML file so I don't need to go fixing one up. Its easier if I can copy/paste and thus saves time.

Study what I'm giving you, because I see a few things that could be edited. Your Host would be best to have a constructor worth using. You should also add threading when dealing with data, and run your code in threads. You should use a delegate with the dispatcher.Invoke method to talk data back to your UI. However, in WPF, you would be best following the MVVM approach on top of what I've already done.
The addRule function adds a template to the ListBox, which works.
In what way does it "work"? That your list contains an observable collection which it doesn't tell you anything about? Why are you using a listbox to hold these objects? Screenshot

Anyway, this is what I changed it too :
C#:
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            Task Begin = new Task(() => addRule());
            Begin.Start();
        }
        public delegate void Del_Callback(object obj);
        private void UpdateUI_WithNewItem(object item)
        {
            hostsListBox.Items.Add(item);
        }
        private void addRule()
        {
            ObservableCollection<Host> hostData = new ObservableCollection<Host>();
            hostData.Add(new Host("Something", "Some address"));
            hostsListBox.Dispatcher.Invoke(new Del_Callback(UpdateUI_WithNewItem), new object[] { hostData });
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            IEnumerable<CheckBox> GetControl_Collection = MyGrid.Children.OfType<CheckBox>();
            Debug.WriteLine($" Found : {GetControl_Collection.Count()} Control(s) of type : {typeof(CheckBox).Name}.");
            CheckBox checkbox = GetControl_Collection.Where(c => c.Name.Equals("checkBox1")).FirstOrDefault();
        }
    }
    public class Host
    {
        public Host(string name, string address)
        {
            Name = name;
            Address = address;
        }

        public string Name
        {
            get;
            set;
        }

        public string Address
        {
            get;
            set;
        }
    }
Specifically finds the control you want:
        private void button_Click(object sender, RoutedEventArgs e)
        {
            IEnumerable<CheckBox> GetControl_Collection = MyGrid.Children.OfType<CheckBox>();
            Debug.WriteLine($" Found : {GetControl_Collection.Count()} Control(s) of type : {typeof(CheckBox).Name}.");
            CheckBox checkbox = GetControl_Collection.Where(c => c.Name.Equals("checkBox1")).FirstOrDefault();
        }
Then check the checkstate :
C#:
            if (checkbox.IsChecked ?? false)
            {
                /* do stuff ie disable buttons */
            }
Implement INotifyPropertyChanged Interface for the checkstate and bind it. If the state is changed, the interface shall instruct to update your UI to set the state of those buttons. Don't be put off by the example linked, it actually doesn't require all that code to do this. Just a few lines.
 
Oh, yea, since your Xaml was incomplete, I used a grid : IEnumerable<CheckBox> GetControl_Collection = MyGrid.Children.OfType<CheckBox>();, so in your case, just set that to be the name of your Stackpanel. ;)
 
Back
Top Bottom