无法将ItemTemplate设置为ListView,因为它找不到资源

时间:2015-12-30 10:43:28

标签: wpf xaml listview

这是我的ListDictionary.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Seminarska">

<DataTemplate x:Name="ArticleListTemplate">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Title}"
                   Margin="5"
                   Style="{StaticResource BodyTextBlockStyle}" />
    </StackPanel>
</DataTemplate>

</ResourceDictionary>

我的MainPage.xaml

<Page
x:Class="Seminarska.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Seminarska"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="80" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <ProgressRing HorizontalAlignment="Center"
                  VerticalAlignment="Center"
                  Visibility="Collapsed"
                  Height="100"
                  Width="100"
                  x:Name="pbLoading"
                  Grid.RowSpan="3" />

    <TextBlock Style="{StaticResource HeaderTextBlockStyle}"
               Grid.Row="0"
               Margin="10,0,0,0"
               Text="My articles"
               VerticalAlignment="Center"
               HorizontalAlignment="Center" />

    <ListView x:Name="lvData"
              Grid.Row="1"
              SelectionChanged="LvData_OnSelectionChanged"
              ItemTemplate="{StaticResource ArticleListTemplate}"
              VerticalAlignment="Stretch"
              HorizontalAlignment="Left"/>
</Grid>
</Page>

正如您所看到的,我正在尝试将ItemTemplate设置为ListView,但它找不到它。它说:

  

资源&#39; ArticleListTemplate&#39;无法解决。

1 个答案:

答案 0 :(得分:1)

假设你没有在应用程序资源中这样做,例如为了从字典文件访问资源,你需要先使用它

<Page>
   <Page.Resources>
      <ResourceDictionary >
         <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/path/to/file/ListDictionary.xaml"/>
         </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
   </Page.Resources>
   <!--  -->
</Page>

而对于DataTemplate,您需要使用x:Key代替x:Name

<DataTemplate x:Key="ArticleListTemplate">