我有一个StackPanel,我需要根据我的数据填写 ExpanderViews 。 我在代码隐藏中创建ExpanderViews并为其分配XAML中存在的 DataTemplate 。
问题是,我能够以编程方式创建ExpanderView,但DataTemplate方法不起作用。我只能看到“Expander Header”,它不会显示点击后的项目。
但是,我可以手动将项目添加到ExpanderView,它会显示Items。
请帮忙!
C#代码:
ExpanderView expandOne = new ExpanderView()
{
Width = 400,
Margin = new Thickness(2),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Expander = new Border()
{
Width = 400,
Background = new SolidColorBrush(Colors.Brown),
Child = new TextBlock()
{
Text = "Expander Header",
FontSize = 34,
Foreground = new SolidColorBrush(Colors.Black),
Margin = new Thickness(40, 5, 5, 5),
},
},
};
// Assign DataTemplate
DataTemplate temp = (DataTemplate)FindName("ItemTemplateName");
expandOne.ItemTemplate = temp;
// add ExpanderView to StackPanel
this.MyStackPanel.Children.Add(expandOne);
XAML代码:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="ItemTemplateKey" x:Name="ItemTemplateName">
<ListBox Grid.Row="0" x:Name="ItemListBox">
<ListBox.Items>
<TextBlock Text="Filter Content 1" Foreground="Black"/>
<TextBlock Text="Filter Content 2" Foreground="Black"/>
<TextBlock Text="Filter Content 3" Foreground="Black"/>
</ListBox.Items>
</ListBox>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>