L
L
lggrlrglgrl2019-07-20 13:34:47
WPF
lggrlrglgrl, 2019-07-20 13:34:47

How to get all elements from wrappanel?

How to access each WrapPanel element and change the Background?
Code example:

public void AddTabMenu()
{
  var lb = new Label();
  var index = wpTopMenu.Children.Count;
  lb.Name = "Scene" + index;
  lb.Content = "Сцена " + index;
  lb.Style = this.Resources["tabMenu"] as Style;
  wpTopMenu.Children.Insert(index - 1, lb);
}

private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
  AddTabMenu();
}

private void Label_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
  if(sender is Label lb)
  {
    var index = wpTopMenu.Children.Count;
    for (int i = 0; i < index; i++)
    {
      var item = wpTopMenu.Children[i];
    }
    lb.Background = new SolidColorBrush(Color.FromArgb(255, 136, 0, 255));

    br.Children.RemoveAt(0);
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cyber_roach, 2019-07-21
@cyber_roach

Assign a Background resource or a style to an element that refers to the Background,
then just change the color of the resource when necessary
Example code:
App.xaml

<Color x:Key="DecorateColor">#FF005087</Color>
<SolidColorBrush x:Key="BackgroundBrush" Color="{DynamicResource DecorateColor}"/>

elements where we change the color, for example, inside the style
change resource color
Color decorateColor = Color.Color.FromArgb(255, 136, 0, 255);
Application.Current.Resources["DecorateColor"] = decorateColor;

If you use MVVM, then you can solve it through StaticResource.
But this is a head-on approach, it will be more true through the style state, but it's up to you to first study Trigger (for this particular case, DataTrigger) and MVVM.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question