【问题标题】:Add rectangle to stackpanel at runtime [duplicate]在运行时将矩形添加到堆栈面板[重复]
【发布时间】:2015-06-28 23:33:26
【问题描述】:

我正在尝试向名为 scorepanel 的命名堆栈面板添加一个矩形,但是当我尝试在我的用户类中使用该名称时,我收到错误“'add' is not a member of '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>

【问题讨论】:

  • 好的。如果我把它放在窗口后面的代码中,我可以让我的代码工作。有什么方法可以从用户类中提取?

标签: wpf vb.net-2010


【解决方案1】:

您应该使用StackPanel.Children.Add 而不是StackPanel.Add

请参阅How to: Instantiate and Use a StackPanel in Code 了解更多信息。

【讨论】:

  • 没有添加矩形。这是我正在使用的代码。
【解决方案2】:

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

具体来说,我会将我的 ItemTemplate 绑定到我的视图模型上的一个可观察集合,该集合将指示要放置在堆栈面板中的矩形数量。

示例”

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多