0

这是我的 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="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>  

FWIW,我在 SeeProductVersions 的构造函数中获得了一个路径,从 Resources 中获取 XmlDataProvider 对象,并根据该路径将一个新的 Uri 推入 Source。

我想在加载所有项目调整列的大小,并且我试图在 ListView 的 Loaded 处理程序中执行此操作,但项目集合当时为空。

是否有适当的事件来处理执行此操作,或者在 XAML 中是否有一种简洁的方法来执行此操作?我确实看到一篇文章提到使用 CellTemplate 并从单元格中捕获 Loaded 事件......

我确信我到处都在违反 MVVM,但由于我对 WPF 和 XAML 还很陌生,所以我不会让这太困扰我!

任何帮助,将不胜感激。

4

1 回答 1

1

好的 - 这是我最终的做法:

<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 非常冗长,并且可能有一种方法可以使用“{}”语句来压缩它,但我不太确定如何。

于 2010-03-08T18:57:27.963 回答