WPF DataTemplate 数据模板放在单独的 XAML 文件中以静态资源的 Source 路径进行访问
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
ResizeMode="CanMinimize"
Title="MainWindow" Height="350" Width="625">
<Window.Resources>
<!--直接路径引用,单个程序集里面其实可以省略路径前面/WpfApp;component/一截-->
<ResourceDictionary Source="/WpfApp;component/Assist/Dictionary1.xaml"/>
</Window.Resources>
<!--窗体内容-->
<StackPanel Orientation="Horizontal" Margin="5">
<UserControl ContentTemplate="{StaticResource carDetailViewTemplate}" Content="{Binding SelectedItem, ElementName=listBoxCars}"/>
<ListBox x:Name="listBoxCars" Width="180" Margin="5 0" ItemTemplate="{StaticResource carListItemViewTemplate}"/>
</StackPanel>
</Window>
<ResourceDictionary Source="/WpfApp;component/Assist/Dictionary1.xaml"/>
引用的是数据模板在项目中的文件路径。和控件模板的方式差不多。
Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp">
<!--Converter-->
<local:AutomakerToLogoPathConverter x:Key="a2l"/>
<local:NameToPhotoPathConverter x:Key="n2p"/>
<!--DataTemplate for Detail View-->
<DataTemplate x:Key="carDetailViewTemplate">
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="6">
<StackPanel Margin="5">
<Image Width="400" Height="250" Stretch="Fill" Source="{Binding Name, Converter={StaticResource n2p}}"/>
<StackPanel Orientation="Horizontal" Margin="5 0">
<TextBlock Text="品牌:" FontWeight="Bold" FontSize="20"/>
<TextBlock Text="{Binding Name}" FontSize="20" Margin="5 0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5 0">
<TextBlock Text="制造商:" FontWeight="Bold"/>
<TextBlock Text="{Binding Automaker}" />
<TextBlock Text="年份:" FontWeight="Bold" Margin="10 0 0 0"/>
<TextBlock Text="{Binding Year}" />
<TextBlock Text="最高速度:" FontWeight="Bold" Margin="10 0 0 0"/>
<TextBlock Text="{Binding TopSpeed, StringFormat={}{0}Km/h}" />
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
<!--DataTemplate for Item View-->
<DataTemplate x:Key="carListItemViewTemplate">
<Grid Margin="2">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Automaker, Converter={StaticResource a2l}}" Grid.RowSpan="3" Width="64" Height="64"/>
<StackPanel Margin="5 10">
<TextBlock Text="{Binding Name}" FontSize="16" FontWeight="Bold"/>
<TextBlock Text="{Binding Year}" FontSize="14"/>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ResourceDictionary>
参考:
《深入浅出 WPF》-- P210
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了