【问题标题】:DataTemplate doesn't work when moved to ResourceDictionary移动到 ResourceDictionary 时,DataTemplate 不起作用
【发布时间】:2019-05-27 00:27:40
【问题描述】:

我试图将 DataTemplate 从 ListView 移动到资源字典中,但它以某种方式破坏了绑定,我相信。

我验证了当我对它在列表视图中显示的 Textblock 文本进行硬编码时,似乎列表视图数据源绑定正在工作,它只是无法显示我的数据。

这是字典:

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

    <DataTemplate x:Key="LVTemplate" x:DataType="local:Result">
        <StackPanel Orientation="Horizontal" Height="Auto" Padding="12" AutomationProperties.Name="{x:Bind ID}">
            <TextBlock Text="{x:Bind ToString()}" VerticalAlignment="Center" Style="{ThemeResource BaseTextBlockStyle}" Foreground="White" Margin="12,0,0,0" FontSize="24"/>
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>

这是我引用模板的方式:

<ListView Grid.Row="1" ItemsSource="{x:Bind VM.Results}" ItemTemplate="{StaticResource LVTemplate}" Background="#FF343434" >           
</ListView>

当我将它放在列表视图模板而不是字典中时,它的外观如下:

<ListView Grid.Row="1" ItemsSource="{x:Bind VM.Results}" Background="#FF343434" >
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="local:Result">
            <StackPanel Orientation="Horizontal" Height="Auto" Padding="12" AutomationProperties.Name="{x:Bind ID}">
                <TextBlock Text="{x:Bind ToString()}" VerticalAlignment="Center" Style="{ThemeResource BaseTextBlockStyle}" Foreground="White" Margin="12,0,0,0" FontSize="24"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

以下是它不在 ResourceDictionary 中时工作的屏幕截图:

这不起作用:

编辑: 这是我的 App.xaml:

<Application
    x:Class="Marathon.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Marathon">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/VCenterTextBox.xaml"/>
                <ResourceDictionary Source="Resources/KeypadButton.xaml"/>
                <ResourceDictionary Source="Resources/ListViewTemplate.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

【问题讨论】:

  • 您尝试使用 Get 添加一个与 ToString() 相同的属性怎么样?可能绑定到一个方法有一些问题,比如它被调用了,结果是空的,然后列表可能会被项目填充,但是一个方法没有被再次调用,所以没有结果。尝试编写您自己的属性,当有一些数据要显示时调用 PropertyChanged。只是我解决这个问题的另一种方法。

标签: c# xaml uwp


【解决方案1】:

在 ResourceDictionary 中使用 x:Bind 时,您需要声明 Dictionary 部分并创建一个代码隐藏文件来实例化它:

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

    <DataTemplate x:Key="LVTemplate" x:DataType="local:Result">
        <StackPanel Orientation="Horizontal" Height="Auto" Padding="12" AutomationProperties.Name="{x:Bind ID}">
            <TextBlock Text="{x:Bind ToString()}" VerticalAlignment="Center" Style="{ThemeResource BaseTextBlockStyle}" Foreground="White" Margin="12,0,0,0" FontSize="24"/>
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>

代码隐藏: 使用 Windows.UI.Xaml.Data;

namespace Marathon.Resources
{
  public partial class ListViewTemplate
  {
    public ListViewTemplate()
    {
      InitializeComponent();
    }
  }
}

App.xaml:

提示:像对象一样实例化模板很重要,以便调用分部类的构造函数和“InitializeComponent()”。不要引用该文件。

<Application
    x:Class="Marathon.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Marathon">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>               
                <local:ListViewTemplate"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

Resource dictionaries with {x:Bind}

【讨论】:

  • 已经在这样做了:/ Intellisense 如果您不添加代码隐藏,则会在 X:Bind 上出现错误,不过感谢您的回复。
  • 请看一下 ResourceDictionary 是如何被引用的。使用:"" 而不是""。您必须像对象一样实例化字典,以便部分类的"InitializeComponent()"被称为
  • 不要引用该文件。
  • 我在 1 周前遇到了同样的问题,我已经用这种方法解决了。打算发布相同的答案。
【解决方案2】:

最好的方法是在你的 App.xaml 中添加引用,然后你就可以在你的应用程序的任何地方使用你的 DataTemplate。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Path/To/Your/ResourceFile/ListViewDataTemplate.xaml" />
            <ResourceDictionary Source="Another/Path/To/Your/ResourceFile/EGButtonTemplateStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

【讨论】:

  • 我的 App.xaml 中已经有了它。我即将用它更新我的帖子。
  • 另外,我没有从智能感知或运行时收到任何错误,它只是不起作用。
  • @superzilla 您是否在运行时更改您的 TextBlock 样式?如果没有,您可以将 ThemeResource 更改为 StaticResource,它可能会解决您的问题
  • 我不相信我是。我基于我在 msdn 上看到的使用 ThemeResource 的 DataTemplate。我只是尝试将其更改为 StaticResource 并从模板中删除 Style 属性,但没有任何更改 - 仍然没有显示。
猜你喜欢
  • 2016-03-26
  • 1970-01-01
  • 2013-11-28
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多