0

I've got a StackPanel which I need to fill in with ExpanderViews based on my data. I am creating the ExpanderViews in code-behind and assigning to it, the DataTemplate present in the XAML.

The problem is, I am able to create the ExpanderView programmatically, but the DataTemplate approach doesn't work. All I can see is the "Expander Header" which doesn't show the items after click.

However, I can add Items manually to the ExpanderView and it shows the Items.

Please help!

C# Code:

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 Code:

<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>
4

1 回答 1

0

如果有人仍在寻找答案,我通过使用中提到的解决方案解决了这个问题

数据绑定到 WP8 Toolkit ExpanderView

于 2013-08-30T12:44:04.423 回答