locked
Access buttons inside a ItemsControl

    Question

  • I have this code in a windows store project.

                <ItemsControl Name="ItemStuff">
                  <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"></StackPanel>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                          <TextBlock Text="lala" Foreground="White" Tapped="SomeFunc"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

    I set the items.source of the ItemsControl with a list of 6 items, When i tap the textblock i change the Foreground color of the text to Red, but i want to set the others to White, how do i access the other buttons, from a tapped event function, so that when i press the text from the third item, and the first on is already Red, it changes the third to Red and the First to White.

    Friday, February 21, 2014 12:09 PM

Answers

  • Hi ,

    Can you try this ?

    You will get a reference for the tb and then change the properties for that button.

    Of course you must write your own code to distinguish the rest of the buttons

    For example in the foreach you must check if the button is already red then change the others.

    So in the tapped event first change the foreground as you are doing right now and then do the loop.

    And inside the loop don't change the "tb" that the foreground color is already changed. You just need an "If" statement

       foreach (Button tb in FindVisualChildren<Button>(this.itemStuff))
                {
                    tb.IsEnabled = true;  //this is an example

                }

     private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj == null)
                {
                    yield break;
                }

                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    var child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (var childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }

    Let me know if it worked

    thank you

    Friday, February 21, 2014 12:42 PM

All replies

  • You could do it i two ways.

    1:

    Store the active TextBlock somewhere and change its Foreground to White before you set the TextBlock you just tapped as active and change its Foreground to Red.

    2:

    You can loop through all the TextBlocks parents children and set their Foreground to White before you change the Forground of the one you tapped to Red

    Friday, February 21, 2014 12:40 PM
  • Hi ,

    Can you try this ?

    You will get a reference for the tb and then change the properties for that button.

    Of course you must write your own code to distinguish the rest of the buttons

    For example in the foreach you must check if the button is already red then change the others.

    So in the tapped event first change the foreground as you are doing right now and then do the loop.

    And inside the loop don't change the "tb" that the foreground color is already changed. You just need an "If" statement

       foreach (Button tb in FindVisualChildren<Button>(this.itemStuff))
                {
                    tb.IsEnabled = true;  //this is an example

                }

     private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj == null)
                {
                    yield break;
                }

                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    var child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (var childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }

    Let me know if it worked

    thank you

    Friday, February 21, 2014 12:42 PM