开发者

findResource() not working with DataTemplate

I defined a DataTemplate the following way:

<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:SurfaceButt开发者_如何转开发on>
            </Grid>
        </DataTemplate>

    </s:SurfaceWindow.Resources>

Then I used it to add a ItemTemplate to a 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>

Later, I add a new ScatterViewItem to the ScatterView:

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);

Unfortunately, I always get a NullReferenceException inthe line:

container.BarView.ItemTemplate = tmpl;

Additional Information:

The object surfaceWindow is passed in this method. It is a reference to the file where i have defined DataTemplate.


Unless your constructor for your LibraryContainer object builds the LibraryContainer.BarView object, it is going to be null at that point.

Edit Ok, so ignore the previous attempts...I've done a little more reading on the surface controls now.

Going back to your original method of fetching the data template via the key:

DataTemplate tmpl = element.FindResource("ContainerItemTemplate") 

If you set a breakpoint there, was the template being returned or was it null at this point?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜