在运行时将矩形添加到stackpanel

时间:2015-04-21 16:47:18

标签: wpf vb.net-2010

我试图将一个矩形添加到一个名为stackpanel的命名堆栈窗口,但是当我尝试在我的用户类中使用该名称时,我得到错误"'添加'不是' Systems.Windows.Controls.StackPanel'"的成员。

如何访问我的记分板?

我需要能够在运行时读取文件并添加矩形。

我能找到的唯一答案是用c#编写的,我无法将它们转换为vb.net。 (我已经使用c#到vb.net转换器以及尝试手动转换。样本在vb.net中无法工作)

以下是我使用的代码。

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

这是我的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>

2 个答案:

答案 0 :(得分:0)

您应该使用StackPanel.Children.Add代替StackPanel.Add

有关详细信息,请参阅How to: Instantiate and Use a StackPanel in Code

答案 1 :(得分:0)

我会使用XAML方法并创建一个ItemTemplate控件。

具体来说,我会将我的ItemTemplate绑定到我的视图模型上的一个可观察的集合,该集合将决定放置在stackpanel中的矩形数。

实施例&#34;

<ItemsControl ItemsSource="{Binding MyRectangles}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Rectangle />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>