在WPF中,我们可能有些一些数据是静态的,但是是一个迭代的列表显示的内容。MSDN建议使用XmlDataProvider。代码如下
<StackPanel x:Name="pn_TaskStatusPanel" Background="#cee8ff" Orientation="Vertical">
<StackPanel.Resources>
<XmlDataProvider x:Key="dataTaskStatus" XPath="/Tasks">
<x:XData>
<Tasks xmlns="">
<Task>
<ImagePath>D:\model1.png</ImagePath>
<Name>XXX</Name>
<Count>20</Count>
</Task>
<Task>
<ImagePath>D:\model1.png</ImagePath>
<Name>XxXX</Name>
<Count>20</Count>
</Task>
<Task>
<ImagePath>D:\model1.png</ImagePath>
<Name>XXX</Name>
<Count>20</Count>
</Task>
<Task>
<ImagePath>D:\model1.png</ImagePath>
<Name>XXXX</Name>
<Count>20</Count>
</Task>
</Tasks>
</x:XData>
</XmlDataProvider>
</StackPanel.Resources>
<ListView x:Name="lv_Status" Height="200" Background="#cee8ff" ItemsSource="{Binding Source={StaticResource dataTaskStatus},XPath=Task}" >
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="#cee8ff"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#d6edff" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Border Height="50" Width="100" Margin="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="3" HorizontalAlignment="Stretch" >
<Grid Margin="3" ShowGridLines="True" HorizontalAlignment="Stretch" >
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="{Binding XPath=ImagePath}" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" />
<Label Content="{Binding XPath=Name}" Grid.Column="1" Grid.Row="0" Grid.IsSharedSizeScope="True" ></Label>
<Label Content="{Binding XPath=Count}" Grid.Column="1" Grid.Row="1"></Label>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackPanel.Resources>
<XmlDataProvider x:Key="dataTaskStatus" XPath="/Tasks">
<x:XData>
<Tasks xmlns="">
<Task>
<ImagePath>D:\model1.png</ImagePath>
<Name>XXX</Name>
<Count>20</Count>
</Task>
<Task>
<ImagePath>D:\model1.png</ImagePath>
<Name>XxXX</Name>
<Count>20</Count>
</Task>
<Task>
<ImagePath>D:\model1.png</ImagePath>
<Name>XXX</Name>
<Count>20</Count>
</Task>
<Task>
<ImagePath>D:\model1.png</ImagePath>
<Name>XXXX</Name>
<Count>20</Count>
</Task>
</Tasks>
</x:XData>
</XmlDataProvider>
</StackPanel.Resources>
<ListView x:Name="lv_Status" Height="200" Background="#cee8ff" ItemsSource="{Binding Source={StaticResource dataTaskStatus},XPath=Task}" >
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="#cee8ff"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#d6edff" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Border Height="50" Width="100" Margin="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="3" HorizontalAlignment="Stretch" >
<Grid Margin="3" ShowGridLines="True" HorizontalAlignment="Stretch" >
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="{Binding XPath=ImagePath}" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" />
<Label Content="{Binding XPath=Name}" Grid.Column="1" Grid.Row="0" Grid.IsSharedSizeScope="True" ></Label>
<Label Content="{Binding XPath=Count}" Grid.Column="1" Grid.Row="1"></Label>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
注意。这里面有一个MSDN提示,XmlDataProvider中注意的地方。
必须声明: xmlns="" 否则 XPath搜寻路径会失灵。
MSDN原文解释。
The root node of the XML data has an xmlns attribute that sets the XML namespace to an empty string. This is a requirement for applying XPath queries to a data island that is inline within the XAML page. In this inline case, the XAML, and thus the data island, inherits the System.Windows namespace. Because of this, you need to set the namespace blank to keep XPath queries from being qualified by the System.Windows namespace, which would misdirect the queries.
· 智能桌面机器人:使用 .NET 为树莓派开发 Wifi 配网功能
· C# 模式匹配全解:原理、用法与易错点
· 记一次SSD性能瓶颈排查之路——寿命与性能之间的取舍
· 理解 .NET 结构体字段的内存布局
· .NET 9中的异常处理性能提升分析:为什么过去慢,未来快
· C# 模式匹配全解:原理、用法与易错点
· 杂七杂八系列----C#代码如何影响CPU缓存速度?
· C#/.NET/.NET Core优秀项目和框架2025年5月简报
· 鸿蒙仓颉语言开发实战教程:商城应用个人中心页面
· FFmpeg开发笔记(六十三)FFmpeg使用vvenc把视频转为H.266编码