xaml - 单独文件夹中的 ListView DataTemplate

标签 xaml windows-10 uwp

我正在尝试学习通用 Windows 平台的编程应用程序。我目前正在与 ListView 合作我在 <DataTemplate> 中定义了它的布局,但代码一团糟。有没有办法定义 <DataTemplate>在单独的文件夹中?我搜索了网络,但我无法找到解决方案。你能帮我解决这个问题吗?谢谢。

最佳答案

我总是建议为这种事情创建一个 ResourceDictionary。这是一个示例设置:

创建一个文件夹 Resources > Add > New item > Resource Dictionary "Templates.xaml"

在你的 App.xaml 添加

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Templates.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

在 Templates.xaml 中,您可以添加任何您想要的模板,如下所示:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:thestory.Resources">


<DataTemplate x:Key="ListItemTemplate">

</DataTemplate>

您现在可以使用 {StaticResource ListItemTemplate} 在任何需要的地方引用此模板

祝你好运!

PS:我实际上还建议对样式和其他应用程序范围的资源(如字体大小、画笔、背景等)执行相同的操作。

关于xaml - 单独文件夹中的 ListView DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754932/

相关文章:

powershell - 如何在操作中心保留PowerShell通知

windows-10 - 尝试为 Windows 10 (x86_64) 静态编译 FFmpeg 4.2.3,但二进制文件要求缺少 DLL

javascript - 解压 JavaScript (p,a,c,k,e,d) 函数

c# - WPF 在运行时更改列宽

c# - 如何使用 mvvm 禁用文本 block ?

c# - 无法绑定(bind)到文本 block - Windows Phone - MVVM

c# - 在 C# UWP 应用程序中存储传感器数据的最佳和最快方式

wpf - Visual Studio : Is there XAML snippets support?

node.js - npm install @types/jquery 导致名称无效 : "@types/jquery"

windows - 当另一个AppWindow在第二个监视器上进入Fullscreen时,如何使UWP AppWindow保持全屏显示?