在WPF中如何从Code Behind访问WrapPanel

时间:2012-07-13 11:49:11

标签: wpf vb.net wpf-controls runtime-error

我在XAML中有如下定义的WrapPanel ...

<Page x:Class="SelectImages"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      mc:Ignorable="d" 
      d:DesignHeight="320" d:DesignWidth="480"
      Title="Select Images">
    <Grid>
        <WrapPanel Name="MyImagePanel" Width="Auto" Height="Auto" Margin="10,50,10,10"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Orientation="Horizontal">

        </WrapPanel>

    </Grid>
</Page>

当我尝试在我的Code Behind中使用WrapPanel时,我得到一个运行时错误“对象引用没有设置为对象的实例”。

Private Sub AddImageToPanel(Image As FileSystemInfo)
    MyImagePanel.Children.Add(NewPicture(Image))
End Sub

谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

我正在使用...

导航到此页面

Dim SelectImagesPage As New SelectImages(FolderPath, SelectedProductID) Me.NavigationService.Navigate(SelectImagesPage)

似乎构造函数不会自动初始化组件。在我的页面上的构造函数中添加了InitializeComponent(),现在一切正常。

相关问题