25

I have the following (very simple) ItemsControl:

<ItemsControl Name="BlahList" ItemsSource="{Binding Blah}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Name="MyCheckBox" Content="{Binding Text}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

In code, I would like to do the following:

foreach (var dahCurrentItem in BlahList.Items)
{
    var ItemCheckBox = BlahList.GimmeMyControl(dahCurrentItem, "MyCheckBox")

    // I'm going to do something with the check box here...
}

How do I do that?

Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
Timothy Khouri
  • 29,873
  • 18
  • 80
  • 125

4 Answers4

28

OK, Kent gets the credit, but it was only mostly right :)

// This part was good:
var container = _itemsControl.ItemContainerGenerator.ContainerFromItem(dahCurrentItem) as FrameworkElement;

but... the second part would return null, so it had to be as follows:

var checkBox = _itemsControl.ItemTemplate.FindName("MyCheckBox", container) as CheckBox;

His code looked like it should have worked, but for my case, I had to do this instead.

Ignatius
  • 1,125
  • 2
  • 23
  • 26
Timothy Khouri
  • 29,873
  • 18
  • 80
  • 125
  • 1
    I don't have a `FindName` method on the ItemTemplate(also there seems to be an extra `)` somewhere? – J4N Apr 18 '16 at 17:05
17

Firstly, don't if there's any way you can avoid it. It's much cleaner to bind the various properties of the CheckBox to your view model rather than trying to pull them out manually.

That said, if you need to get to your CheckBox, you can should be able to use code like this:

var container = _itemsControl.ItemContainerGenerator.ContainerFromItem(dahCurrentItem) as FrameworkElement;
var checkBox = container.FindName("MyCheckBox") as CheckBox;
Tim Cooper
  • 144,163
  • 35
  • 302
  • 261
Kent Boogaart
  • 165,446
  • 34
  • 376
  • 376
  • You sir, are beautiful! - I agree with your statement, but in this case I'm doing a "Check All" and "Check None" type function... I realize I could have the "isChecked" bound to some array, and then re-update bindings... but just foreaching and checking is lazier... I mean.. easier :) Thanks again! – Timothy Khouri Mar 02 '09 at 19:14
  • 2
    OK, I had to take away the "answered" check box... because I'm only getting "NULL". I imagine this is really close, but there must be something small missing. – Timothy Khouri Mar 02 '09 at 19:46
  • What should be done finally to not get null when doing the `FindName`? – J4N Apr 18 '16 at 17:11
0

Here's an example of capturing a container that houses your ItemsControl's item:

       CheckBox checkbox = sender as CheckBox;

        foreach (var item in MembersItemsControl.Items)
        {
            var container = 
MembersItemsControl.ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement;

            UserInformation user = container.DataContext as UserInformation;

            bool isMale = true;
            if (user.sex == isMale && checkbox.IsChecked.Value == true)
            {
                container.Visibility = System.Windows.Visibility.Visible;
            }
        }

I hope that helps.

Brandon
  • 65,640
  • 30
  • 189
  • 218
Scott Nimrod
  • 10,451
  • 8
  • 46
  • 94
0

I used the code that Kent and Timothy supplied, but this additional line was also needed for me.

The whole code snippet goes:

var container = _itemsControl.ItemContainerGenerator.ContainerFromItem(dahCurrentItem) as FrameworkElement;
container.ApplyTemplate()
var checkBox = _itemsControl.ItemTemplate.FindName("MyCheckBox", container) as CheckBox;

Cheers!

mZm
  • 128
  • 7
  • I re-framed the answer to not look like a question. I wanted only to add the line that was not in previous answers. Also, as you said I don't have enough reputation for a comment... – mZm May 08 '19 at 14:00
  • answers need to be standalone, they must not depend on other answers – Jean-François Fabre May 08 '19 at 19:16