我使用ItemTemplate和DataTemplate创建了一个简单的ItemsControl。
我将一个填充的IEnumerable分配给DataContext,但没有显示任何项目。
这是我的ItemsControl(XAML):
<ItemsControl Name="pnlMenu" DataContext="{Binding}"
Grid.Row="0" Margin="5,17,0,28">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button BorderBrush="{x:Null}" BorderThickness="0" Background="DodgerBlue" Height="25" Width="185" Margin="5,0" RenderTransformOrigin="0.5,0.5">
<Button.ContentTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Width="170">
<Image Source="Assets/Square71x71Logo.scale-100.png" Width="35" />
<TextBlock Text="{Binding Title}" VerticalAlignment="Center" Margin="5,0,0,0" />
</StackPanel>
</DataTemplate>
</Button.ContentTemplate>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是背后的代码(C#):
public void SetMenuDataContent(IEnumerable<NavDataGroup> groups)
{
pnlMenu.DataContext = groups;
}
这可能听起来很奇怪,但我实际上是从WP8.1转到WP8而不是相反。
这曾经在WP8.1中运行,但为什么在WP8中这不起作用? MSDN向我展示了类似的例子......
以下是IEnumerable中的一个组的示例:
{
"Groups":[
{
"UniqueId": "mainpage",
"Title": "Start",
"Subtitle": "U bent hier thuis",
"ImagePath": "Assets/DarkGray.png",
"Description": "Een snel overzicht...",
"Items":
[
{
"UniqueId": "outdoors",
"Title": "Buitenbad",
"Subtitle": "Het buitenbad is vandaag",
"ImagePath": "Assets/Open.png",
"Description" : "",
"Content" : "Open"
},
{
"UniqueId": "indoors",
"Title": "Binnenbad",
"Subtitle": "Het binnenbad is vandaag",
"ImagePath": "Assets/Open.png",
"Description" : "",
"Content" : "Open"
},
{
"UniqueId": "photos",
"Title": "Foto's",
"Subtitle": "",
"ImagePath": "Assets/LightGray.png",
"Description" : "",
"Content" : "[\"Assets/LightGray.png\", \"Assets/DarkGray\", \"Assets/MediumGray\"]"
}
]
}
]
}
答案 0 :(得分:2)
这是一个适合我的解决方案:
public async void SetMenuDataContent()
{
this.DataContext = groups;
}
我直接绑定到UserControl的DataContext。 当我尝试绑定到UserControl和UserControl本身内的控件时发生问题。显然是一个容易犯的错误。