0

I'm trying to add a rectangle to a named stackpanel, called scorepanel but when I try to use the name in my user class I get the error the "'add' is not a member of 'Systems.Windows.Controls.StackPanel'".

How can I access my scorepanel?

I need to be able to read a file and add rectangles at runtime.

The only answers I've been able to find are written in c# and I can't convert them to vb.net. (I've used c# to vb.net converter as well as trying to convert by hand. The samples won't work in vb.net)

Here is code I'm using.

Class MainWindow

    Public Sub ini()
         Dim a As New SetBackgroundColorOfShapeExample
    End Sub
End Class

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes

Partial Public Class ScoreSystem
    Inherits MainWindow

    Public Sub DrawRect()
        Dim rect As New Rectangle
        Dim mySolidColorBrush As New SolidColorBrush()

        mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0)

        With rect
            .MinHeight = 96
            .MaxHeight = 96
            .Height = 96
            .MinWidth = 6 * 96
            .MaxWidth = 6 * 96
            .Width = 6 * 96
            .Fill = mySolidColorBrush
        End With
        ScorePanel.Children.Add(rect)
    End Sub
End Class 'System

Here is my xaml.

    <StackPanel>
        <Menu  Name="Menu"    DockPanel.Dock="Top">
            <MenuItem Header="_File" Name="MnuFile">
            <MenuItem Header="_Color Configuration" Click="ColorDialogShow_Click"/>
    </Menu>

    <DockPanel x:Name="Document" Width="8.5 in" HorizontalAlignment="Center" Background="Azure">
        <Grid x:Name="Page" Width="8.5 in" Height="11 in">
               <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
            <StackPanel>

                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <TextBox x:Name="ScoreTitle" Grid.Column="0" HorizontalAlignment="Stretch" TextWrapping="Wrap"  
                     FontSize="28" FontWeight="Bold" Text="TITLE" VerticalAlignment="Stretch" Margin="0,5,0,0" 
                     BorderThickness="0" TextAlignment="Center" Background="Transparent"/>

                         <TextBox x:Name="PageNumber" Grid.Column="2" HorizontalAlignment="Right" TextWrapping="Wrap" 
                         FontSize="12" FontWeight="Bold" Text="Page 1" VerticalAlignment="Top" Margin="0,5,5,0" 
                         BorderThickness="0" TextAlignment="Center" Background="Transparent" Grid.RowSpan="2"/>

       </Grid>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <!--<ColumnDefinition Width="Auto"/>-->
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                <TextBox x:Name="Textbox0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Bottom" TextWrapping="Wrap" 
                         FontSize="12" FontWeight="Bold" Text="Text" BorderThickness="0" TextAlignment="Left" Background="Transparent"  
                         Margin="5,0,0,0"/>

                <TextBox x:Name="Composer" Grid.Column="2" HorizontalAlignment="Center" TextWrapping="Wrap" 
                         FontSize="12" FontWeight="Bold" Text="Composer" VerticalAlignment="Bottom" Margin="62,0,61,5" 
                       BorderThickness="0" TextAlignment="Center" Background="Transparent"/>
           </Grid>
        </StackPanel>
            <Grid Grid.Row="2">
                <StackPanel x:Name="ScorePanel" HorizontalAlignment="Left" Margin="-2,0,0,-404" Grid.Row="1" Width="8.5 in" Height="11 in" 
                VerticalAlignment="Bottom">
'2 Grid element to prove visual compliance.
                    <Grid Height="1.5 in" Width=" 6 in" HorizontalAlignment="Center" Background="Pink">
                        <WrapPanel Height="Auto" Width="Auto" HorizontalAlignment="Right" VerticalAlignment="Center">
                            <Rectangle Name="_0" Fill="#FFFF0034" Height="1 in" Width="2 in"/>
                            <Rectangle Name="_1" Fill="#FFE200FF" Height="1 in" Width="2 in" VerticalAlignment="Center"/>
                            <Rectangle Name="_2" Fill="#FFFF2800" Height="1 in" Width="2 in" VerticalAlignment="Center"/>
                        </WrapPanel>
                    </Grid>

                    <Grid Height="1.5 in" Width=" 6 in" HorizontalAlignment="Center" Background="Violet">
                        <WrapPanel Height="1 in" Width="6 in" HorizontalAlignment="Right" VerticalAlignment="Center">
                            <Rectangle Name="_3" Fill="#FF00FF1E" Height="1 in" Width="2 in" />
                            <Rectangle Name="_4" Fill="#FF1D00FF" Height="1 in" Width="2 in"/>
                            <Rectangle Name="_5" Fill="#FF00F9FF" Height="1 in" Width="2 in"/>
                        </WrapPanel>
                    </Grid>
              </StackPanel>
                         <Button x:Name="button" Content="Draw Rectangle" Width="Auto" Height="40
                                 " Click="Draw"/>
       </Grid>   

        </Grid>
    </DockPanel>
  </StackPanel>

</Window>
Don6558
  • 351
  • 1
  • 4
  • 11
  • Ok. I can get my code to work if I place it in the code behind window. Is there any way to draw from a user Class? – Don6558 Apr 22 '15 at 05:29

2 Answers2

0

You should be using StackPanel.Children.Add instead of StackPanel.Add

See How to: Instantiate and Use a StackPanel in Code for more info.

d.moncada
  • 16,900
  • 5
  • 53
  • 82
0

I would use the XAML approach and create an ItemTemplate control.

Specifically, I would bind the my ItemTemplate to an observable collection on my view-model that will dictate the number of rectangles to place in the stackpanel.

Example"

<ItemsControl ItemsSource="{Binding MyRectangles}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Rectangle />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118