jquery_stack
jquery_stack

Reputation: 261

Row of StackPanels

I'm creating a WPF application that contains 100 individual StackPanels that have been placed at a specific position manually, assigning to each one a starting background color. The XAML part looks like this:

<StackPanel Height="300" HorizontalAlignment="Left" Margin="228,120,0,0" Name="stackPanel1"
   VerticalAlignment="Top" Width="6" Background="Red" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="234,120,0,0" 
   Name="stackPanel2" VerticalAlignment="Top" Width="6" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="240,120,0,0" 
   Name="stackPanel3" VerticalAlignment="Top" Width="6" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="246,120,0,0" 
   Name="stackPanel4" VerticalAlignment="Top" Width="6" />
<StackPanel Background="Red" Height="300" HorizontalAlignment="Left" Margin="252,120,0,0" 
   Name="stackPanel5" VerticalAlignment="Top" Width="6" />

As it can be seen, the Background is Red for the first 5 ones, and the Width is 6 (the Margin of each one is placed 6 units after the other to make them attached).

Now, what I want is to for example, at the .cs be able to handle them by creating a row containing them (more or less like [stackPanel1, stackPanel2, ..., stackPanel100]). I want to do that to, for example, change the color of the first 5 ones with the following code:

for(int i=0;i<100;i++){
    if(i<10){
        stackPanelsRow[i].Background = Colors.Blue; //Change the background color to blue
    }
    if(i>10 && i<20){
        stackPanelsRow[i].Background = Colors.Green; //Change the background color to green
    }
    //...
}

How can I construct this row of StackPanels if they are individual entities created at the Designer and placed manually according to the conditions expressed above?

enter image description here

Upvotes: 1

Views: 2969

Answers (2)

galakt
galakt

Reputation: 1439

Wrap your stack panels by StackPanel

<StackPanel x:Name="StackPanelTest" Orientation="Horizontal">
    <StackPanel/>
    <StackPanel/>
    <StackPanel/>
</StackPanel>

And you can access like

var someStackPanel = StackPanelTest.Children[0] as StackPanel;

Upvotes: 0

Salah Akbari
Salah Akbari

Reputation: 39966

In WPF you can use Style for this purpose:

<Window.Resources>
    <Style TargetType="StackPanel">
        <Setter Property="Background" Value="Red"></Setter>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel ... /> <!--You don't need to set background here anymore-->
    <StackPanel ... />
    ....
</Grid>

If you don't want to assign the same color for all the elements of the same type you can use x:Key of the style and then apply to each StackPanel that you want:

<Style TargetType="StackPanel" x:Key="MyStyle">

And then:

<StackPanel Style="{StaticResource MyStyle}" ... />


Edit: Another option for doing this would be like this:

<Grid x:Name="mainGrid">
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>

    </Grid.RowDefinitions>
    <StackPanel Name="stackPanel1"  Width="6" />
    <StackPanel Grid.Row="1"  Height="300" Name="stackPanel2"  Width="6" />
    <StackPanel Grid.Row="2"  Height="300" Name="stackPanel3"  Width="6" />
    <StackPanel Grid.Row="3"  Height="300" Name="stackPanel4"  Width="6" />
    <StackPanel Grid.Row="4"  Height="300" Name="stackPanel5"  Width="6" />
</Grid>

Then in the code-behind:

foreach (var stackpanl in mainGrid.Children.OfType<StackPanel>())
{
     if (Grid.GetRow(stackpanl) < 2)
     {
          stackpanl.Background = new SolidColorBrush(Colors.Blue);                    
     }
}

Upvotes: 1

Related Questions