2

My code is as below.

<ListBox x:Name="lstBoxMarket"  BorderThickness="0" Height="Auto" HorizontalAlignment="Center"  Width="200" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
    <ListBox.ItemTemplate>
        <HierarchicalDataTemplate>
            <CheckBox IsChecked="{Binding Checked}"  CommandParameter="{Binding MarketId}" Tag="{Binding MarketId}" Content="{Binding Market}"  Foreground="#FF3D66BE" Name="chkMarket"/>
        </HierarchicalDataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I want to access the selected and deselected checkboxes in the list on click of save button . I am unable to access chkMarket straight away. Can anyone help?

1
  • 2
    You have the {Binding Checked} statement in the code which means that you bind it to a view model and you store the list of them somewhere. What is the DataContext of lstBoxMarket? Commented Mar 12, 2012 at 6:34

2 Answers 2

1

Starting from your code I tried something like that

                 // find all T in the VisualTree
                 public static IEnumerable<T> FindVisualChildren<T>(DependencyObject parent) 
        where T : DependencyObject
    {
        List<T> foundChilds = new List<T>();

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);

            T childType = child as T;
            if (childType == null)
            {
                foreach(var other in FindVisualChildren<T>(child))
                    yield return other;
            }
            else
            {
                yield return (T)child;
            }
        }
    }

Then in your MainWindow

    private void button1_Click(object sender, RoutedEventArgs e)
    {
                           // find all checkboxes in my window
        IEnumerable<CheckBox> myBoxes = FindVisualChildren<CheckBox>(this);

        int numChecked = 0;
        foreach(CheckBox cb in myBoxes)
        {
            if(cb.Name != "chkMarket")
                continue;


            if (cb.IsChecked == true)
                numChecked++;

        }

        MessageBox.Show("Checked items = " + numChecked);


    } 

My viewmodel code is

   public class ViewModel
{
    public ViewModel()
    {
        _persons = new ObservableCollection<Person>();
        _persons.Add(new Person() { Name = "Paul", Checked = false });
        _persons.Add(new Person() { Name = "Brian", Checked = true });
    }

    private ObservableCollection<Person> _persons;

    public ObservableCollection<Person> Persons
    {
        get { return _persons; }
    }
}

public class Person
{
    public String Name { get; set; }
    public Boolean Checked { get; set; }
}

You should be able to see the message "Checked items=1". Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

Whenever you are using the VisualTreeHelper chances are that you are doing something wrong. In this case one could easily access the needed value via the bound objects, direct access to the controls is rarely ever needed in WPF.
1

Since it was 2 way binding i could access the values selected by the checkboxes from the item source of listbox. DataTable lstBoxMarketItemSourceDT = ((DataView)lstBoxMarket.ItemsSource).ToTable();

"Checked" column in the data table retrieved gives the updated check box values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.