Reputation: 259
I'm working on a C++ Metro style app and have a problem with the binding inside an ItemTemplate of a ListView (or its items respectively). If I do it right in my Page.xaml it is working. The (simplified) code would be:
<ListView x:Name="m_listParts" ItemsSource="{Binding PartsList}>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="60" Height="60">
<Grid>
<TextBlock Text="{Binding Part}"/>
</Grid>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
However, I would like to have the ItemTemplate definition in my resourceDictionary. But I cannot figure out how to get the binding working. It just seems to not find the bound properties anymore.
Here is my (simplified) try (since the ItemsPanel is working I suppose I loaded the dictionary itself properly):
<Style x:Key="PartsListListView" TargetType="ListView">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="60" Height="60">
<Grid>
<TextBlock Text="{Binding Part}"/>
</Grid>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
The PartsList is an observable vector holding PartViewItem objects which consists of a Part property.
Upvotes: 1
Views: 3796
Reputation: 43011
You should write your Xaml as
<ListView
ItemsPanel="{StaticResource MyItemsPanel}"
ItemTemplate="{StaticResource MyItemTemplate}" .../>
where you have resources
<UserControl.Resources>
<DataTemplate x:Key="MyItemTemplate" DataType="{x:Type MyItemType}">
<StackPanel ....
</DataTemplate>
<ItemsPanelTemplate x:Key="MyItemsPanel">
<StackPanel...
</ItemsPanelTemplate>
</UserControl.Resources>
Upvotes: 2