Listview中的ItemsTemplate未通过Page.Resources显示

时间:2017-01-13 16:55:24

标签: c# xaml listview uwp

任何人都可以帮助我。我正在尝试在内容对话框中显示列表视图。 最终,我将使用Bindings,但为了排除故障,我只想看到一个带有“test”字样的文本框。这是一些代码:

MainPage.xaml中

    <Page.Resources>
    <DataTemplate x:Key="MyUploadsDialogTemplate">
        <Grid>
            <StackPanel>
                <RelativePanel>

                    <TextBlock x:Name="MyArtistTextBox" Width="150" TextAlignment="Center" Text="TEST" FontSize="12"></TextBlock>


                </RelativePanel>
            </StackPanel>
        </Grid>
    </DataTemplate>
</Page.Resources>

然后我有一个我的contentdialogbox的课程:

class MyUploadsDialog
{
    public static ListView MyUploadsListView = new ListView() { Height = 375, Width = 410, Margin = new Thickness(0, 0, 0, 0), Background = new SolidColorBrush(Color.FromArgb(255, 48, 179, 221)) };

    public static Panel MyUploadsPanel = new StackPanel();
    public static ContentDialog UploadsDialog = new ContentDialog() { Title = "My Music Uploads" };

    //Constructor
    public MyUploadsDialog()
    {

        Initialize();

    }
    public void Initialize()
    {


        MyUploadsPanel.Children.Add(new TextBlock
        {
            Text = "As an artist or label, you will see you music uploads here with some basic info about them.",
            TextWrapping = TextWrapping.Wrap,
        });



        MyUploadsPanel.Children.Add(MyUploadsListView);

        UploadsDialog.Content = MyUploadsPanel;

        UploadsDialog.PrimaryButtonText = "Close";
        UploadsDialog.IsPrimaryButtonEnabled = true;

    }
}

最后,我将page.resources DataTemplate分配给我的MainPage()构造函数中的ListView.ItemTemplate:

MyUploadsDialog.MyUploadsListView.ItemTemplate = (DataTemplate)Resources["MyUploadsDialogTemplate"];

结果是我看到了蓝色的ListView背景......但是我没有在Listview中看到带有“test”字样的TextBox。

我没有得到任何错误或异常,它只是不会显示DataTemplate代码。

任何帮助都会很棒。

更新:我在XAML中的典型列表视图

<ListView x:Name="NewMusicListView"  Height="275" Width="410" Margin="20,0,0,0" RelativePanel.RightOf="MusicCountBox" RelativePanel.Below="Blurb" IsItemClickEnabled="True" SelectionChanged="NewMusicListView_SelectionChanged">

                    <ListView.HeaderTemplate>
                        <DataTemplate>
                            <Grid>
                                <StackPanel>
                                    <RelativePanel>

                                        <TextBlock x:Name="ArtistHeader" Padding="25,0,0,0" Width="150" TextAlignment="Center" Text="Artist Name" FontSize="18" Foreground="#ff6600"></TextBlock>


                                        <TextBlock x:Name="SongHeader"   Padding="25,0,0,0" Width="150" TextAlignment="Center" Text="Song Title" RelativePanel.RightOf="ArtistHeader" FontSize="18" Foreground="#ff6600"></TextBlock>
                                    </RelativePanel>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ListView.HeaderTemplate>

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <StackPanel>
                                <RelativePanel>

                                        <TextBlock x:Name="ArtistTextBox" Width="150" TextAlignment="Center" Text="{Binding Artist}" FontSize="12"></TextBlock>
                                        <TextBlock x:Name="SongTextBox" Width="150" TextAlignment="Center" Text="{Binding Song}" RelativePanel.RightOf="ArtistTextBox" FontSize="12"></TextBlock>


                                </RelativePanel>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
                </ListView>

1 个答案:

答案 0 :(得分:0)

好的我明白了。谢谢你的帮助。我一直在努力工作,以至于我的大脑变得糊里糊涂。我确实使用ItemsSource作为我的其他listview绑定...所以现在我已经添加了...它正在工作。

 MyUploadsListView.ItemsSource = MyUploadsList;

其中列表是可观察的集合。

感谢您的帮助!