0

Below is the markup that generates a list of buttons.

 <ItemsControl x:Name="Items" Grid.Row="5"  Grid.ColumnSpan="2">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ToggleButton Content="{Binding Name}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

There is filtering applied to the buttons using a filtring criteria

 collectionView = (CollectionView)CollectionViewSource.GetDefaultView(Items.ItemsSource);
 collectionView .Filter = FilterList;

The problem is that i want to retain states of button checked when i toggle the filter state. I have tried subscribing to the event StatusChanged

Items.ItemContainerGenerator.StatusChanged += new System.EventHandler(ItemContainerGenerator_StatusChanged);

but the controls dont seem to be generated at the point when status is ContainersGenerated

void ItemContainerGenerator_StatusChanged(object sender, System.EventArgs e)
{
  if (Items.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
            {
                RefreshButtons();
            }    
}
TrustyCoder
  • 4,749
  • 10
  • 66
  • 119

1 Answers1

0

The only way to access them is to use the VisualTree. Just use something like this:

public static T[] FindVisualChilds<T>(DependencyObject parent, Func<DependencyObject, bool> CompareDelegate)
    where T : DependencyObject
{
    if (VisualTreeHelper.GetChildrenCount(parent) == 0) return null;
    List<T> childs = new List<T>();

    if (CompareDelegate(parent))
        childs.Add(parent as T);

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
    {
        var tmp = FindVisualChilds<T>(VisualTreeHelper.GetChild(parent, i), CompareDelegate);
        if (tmp != null)
            childs.AddRange(tmp);
    }
    return childs.ToArray();
}

Now pass as firstparameter your datagrid and as second a delegate that checks if the control is the control you want. As result you will get all control those are found

Florian
  • 5,918
  • 3
  • 47
  • 86
  • This does not work when the event ItemContainerGenerator_StatusChanged gets fired. there are no controls in the visual tree. Am I missing something here? – TrustyCoder Aug 10 '12 at 13:52