Я определил DataTemplate следующим образом:FindResource() не работает с 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>
Позже я добавить новый ScatterViewItem к 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);
к сожалению, я всегда получаю NullReferenceException Inthe линии:
container.BarView.ItemTemplate = tmpl;
Дополнительная информация:
Объект surfaceWindow передается в этом методе. Это ссылка на файл, где я определил DataTemplate.
Спасибо. Поэтому я добавил контейнер line.BarView = новый BarView(); Теперь он работает !. Что вы имеете в виду с вашим боковым узлом? Я действительно не понимаю свойство DataType. – RoflcoptrException
В основном шаблон данных будет применяться с использованием типа данных, поэтому вам не нужно связывать тип шаблона. –
Если я изменяю это и удаляю привязку здесь: , тогда шаблон не применяется: / –
RoflcoptrException