0

我通过以下方式定义了一个 DataTemplate:

<s:SurfaceWindow.Resources>
    <ImageBrush x:Key="WindowBackground" Stretch="None" Opacity="0.6" ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>

        <DataTemplate x:Key="ContainerItemTemplate">
            <Grid>
                <Border BorderThickness="1" BorderBrush="White" Margin="3">
                    <s:SurfaceTextBox IsReadOnly="True" Width="120" Text="{Binding Path=name}" Padding="3"/>
                </Border>
                <s:SurfaceButton Content="Expand" Click="SourceFilePressed"></s:SurfaceButton>
            </Grid>
        </DataTemplate>

    </s:SurfaceWindow.Resources>

然后我用它来将 ItemTemplate 添加到 LibraryContainer:

<Grid Name="RootGrid" Background="{StaticResource WindowBackground}" >
        <s:ScatterView Name="RootScatter">
            <Viewbox>
                <s:LibraryContainer Name="RootContainer" Grid.Row="0" ViewingMode="Bar">
                    <s:LibraryContainer.BarView>
                        <s:BarView Rows="2" NormalizedTransitionSize="2.5,0.8" ItemTemplate="{StaticResource ContainerItemTemplate}">
                        </s:BarView>
                    </s:LibraryContainer.BarView>
                    <s:LibraryContainer.StackView>
                        <s:StackView NormalizedTransitionSize="1,1" ItemTemplate="{StaticResource ContainerItemTemplate}">
                        </s:StackView>
                    </s:LibraryContainer.StackView>
                </s:LibraryContainer>
            </Viewbox>
        </s:ScatterView>
    </Grid>

稍后,我在 ScatterView 中添加了一个新的 ScatterViewItem:

ScatterViewItem item = new ScatterViewItem();

                FrameworkElement element = surfaceWindow as FrameworkElement;
                DataTemplate tmpl = element.FindResource("ContainerItemTemplate") as DataTemplate;


                LibraryContainer container = new LibraryContainer();
                container.ViewingMode = LibraryContainerViewingMode.Bar;
                container.ItemsSource = itms;
                container.BarView.ItemTemplate = tmpl;

                item.Content = container;
                surfaceWindow.getRootScatter().Items.Add(item);

不幸的是,我总是在行中得到一个 NullReferenceException :

container.BarView.ItemTemplate = tmpl;

附加信息:

对象surfaceWindow 在此方法中传递。它是对我定义 DataTemplate 的文件的引用。

4

1 回答 1

3

除非您的 LibraryContainer 对象的构造函数构建 LibraryContainer.BarView 对象,否则此时它将为空。

编辑 好的,所以忽略之前的尝试......我现在已经对表面控件进行了更多阅读。

回到通过密钥获取数据模板的原始方法:

DataTemplate tmpl = element.FindResource("ContainerItemTemplate") 

如果您在此处设置断点,那么模板是返回还是此时为空?

于 2010-10-27T00:55:55.767 回答