通过与XmlDataProvider绑定加载WPF ListView,我想在创建项后调整列的大小
这是我的XAML:通过与XmlDataProvider绑定加载WPF ListView,我想在创建项后调整列的大小,wpf,xaml,listview,xmldataprovider,Wpf,Xaml,Listview,Xmldataprovider,这是我的XAML: <Window x:Class="Application.SeeProductVersions" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Application.Applicatio
<Window x:Class="Application.SeeProductVersions"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Application.ApplicationData"
Title="Product Versions" Height="300" Width="640" Loaded="Window_Loaded">
<Window.Resources>
<XmlNamespaceMappingCollection x:Key="VersionDataNamespaceMapping">
<XmlNamespaceMapping Uri="http://whereever.com/VersionData" Prefix="vdata" />
</XmlNamespaceMappingCollection>
<XmlDataProvider x:Key="ProductDataXmlFile" XmlNamespaceManager="{StaticResource VersionDataNamespaceMapping}"></XmlDataProvider>
</Window.Resources>
<ListView x:Name="m_lvProductVersions" ItemsSource="{Binding Source={StaticResource ProductDataXmlFile}, XPath=//vdata:PRODUCTDATA/vdata:PRODUCT}" Loaded="m_lvProductVersions_Loaded">
<ListView.View>
<GridView>
<GridViewColumn x:Name="colProduct" Width="64" Header="Product" DisplayMemberBinding="{Binding XPath=vdata:NAME}"/>
<GridViewColumn x:Name="colVersion" Width="64" Header="Version" DisplayMemberBinding="{Binding XPath=vdata:VERSION}"/>
<GridViewColumn x:Name="colLink" Width="256" Header="Download Link" DisplayMemberBinding="{Binding XPath=vdata:LINK}"/>
</GridView>
</ListView.View>
</ListView>
</Window>
以下是一些示例XML:
<?xml version="1.0" encoding="utf-8"?>
<vdata:PRODUCTDATA xmlns:vdata="http://whereever.com/VersionData">
<vdata:PRODUCT>
<vdata:ID>04</vdata:ID>
<vdata:NAME>ProductWithALongName</vdata:NAME>
<vdata:VERSION>8.7.12.0</vdata:VERSION>
<vdata:LINK>http://www.whereever.com/support/LongNames/ProductWithALongName-download.asp</vdata:LINK>
</vdata:PRODUCT>
<vdata:PRODUCT>
<vdata:ID>07</vdata:ID>
<vdata:NAME>ModerateName</vdata:NAME>
<vdata:VERSION>9.12.5.0</vdata:VERSION>
<vdata:LINK>http://www.whereever.com/support/ModerateNames/ModerateName-download.asp</vdata:LINK>
</vdata:PRODUCT>
<vdata:PRODUCT>
<vdata:ID>16</vdata:ID>
<vdata:NAME>ShortName</vdata:NAME>
<vdata:VERSION>9.9.19.0</vdata:VERSION>
<vdata:LINK>http://www.whereever.com/support/ShortNames/ShortName-download.asp</vdata:LINK>
</vdata:PRODUCT>
</vdata:PRODUCTDATA>
04
ProductWithongName
8.7.12.0
http://www.whereever.com/support/LongNames/ProductWithALongName-download.asp
07
中间胺
9.12.5.0
http://www.whereever.com/support/ModerateNames/ModerateName-download.asp
16
短名
9.9.19.0
http://www.whereever.com/support/ShortNames/ShortName-download.asp
FWIW,我在SeeProductVersions的构造函数中获得一个路径,从资源中获取XmlDataProvider对象,并根据该路径将一个新的Uri推入源代码
我想在加载所有项后调整列的大小,我试图在ListView的已加载处理程序中执行此操作,但items集合当时为空
是否有适当的事件来处理以执行此操作,或者是否有一种简洁的方式在XAML中执行此操作?我确实看到一篇文章提到使用CellTemplate并从cells捕获加载的事件
我确信我正在到处违反MVVM,但因为我对WPF和XAML还很陌生,所以我不会让它太困扰我
任何帮助都将不胜感激。好的-以下是我如何完成这项工作的:
<Window x:Class="Application.SeeProductVersions"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Application.ApplicationData"
Title="Product Versions" Height="300" Width="720" Loaded="Window_Loaded">
<Window.Resources>
<XmlNamespaceMappingCollection x:Key="VersionDataNamespaceMapping">
<XmlNamespaceMapping Uri="http://whereever.com/VersionData" Prefix="vdata" />
</XmlNamespaceMappingCollection>
<XmlDataProvider x:Key="ProductDataXmlFile" XmlNamespaceManager="{StaticResource VersionDataNamespaceMapping}"/>
</Window.Resources>
<ListView x:Name="m_lvProductVersions" ItemsSource="{Binding Source={StaticResource ProductDataXmlFile}, XPath=//vdata:PRODUCTDATA/vdata:PRODUCT}" Loaded="m_lvProductVersions_Loaded">
<ListView.View>
<GridView>
<GridViewColumn Header="Product">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="Auto" Text="{Binding XPath=vdata:NAME}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Version">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="Auto" Text="{Binding XPath=vdata:VERSION}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Download">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink NavigateUri="{Binding XPath=vdata:LINK}" Click="DownloadHyperlink_Click">
<TextBlock Width="Auto" Text="{Binding XPath=vdata:LINK}"/>
</Hyperlink>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Window>
作为额外的一点好处,最后一列将显示为超链接,我在代码后面有一个处理程序
我知道XAML非常冗长,可能有一种方法可以使用“{}”语句对其进行压缩,但我不太确定如何压缩。我想,既然我知道数据来自何处,并且可以访问它,那么我就可以预解析它并计算出宽度。此外,我看到集合中的“项目”实际上是元素…或类似的东西。