views:

331

answers:

1

Here is my 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>  

Here is some sample XML:

<?xml version="1.0" encoding="utf-8"?>  
  <vdata:PRODUCTDATA xmlns:vdata="http://whereever.com/VersionData"&gt;  
    <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&lt;/vdata:LINK&gt;  
    </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&lt;/vdata:LINK&gt;  
    </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&lt;/vdata:LINK&gt;  
    </vdata:PRODUCT>  
  </vdata:PRODUCTDATA>  

FWIW, I get a path in the constructor for SeeProductVersions, grab the XmlDataProvider object from the Resources and shove a new Uri based on the path into Source.

I want to resize the columns after all the items have been loaded, and I was trying to do it in the Loaded handler for the ListView, but the items collection was empty then.

Is there an appropriate event to handle to perform this operation, or is there a neat way to do it in XAML? I did see one post that mentioned using a CellTemplate and catching the Loaded event from the cells....

I am sure I am violating MVVM all over the place, but since I am pretty new to WPF and XAML, I am not going to let that bother me too much!

Any help would be appreciated.

A: 

OK - here is how I ended up doing it:

<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>  

As an added little bonus, the last column will display as a HyperLink, and I have a handler in the code-behind.

I know the XAML is very verbose, and there is probably a way to condense it using "{}" statements, but I am not quite sure how.

GTAE86