views:

683

answers:

1

I'm trying to do some WPF databinding, but I'm a little hung up at the moment. I have two listboxes and an XML file. The first listbox successfully binds to the XML source. However, when I try to bind to a child of the selected item from first listbox as the source for the second list box, nothing appears. The goal being something like an index or look-up (selecting one index results in finding the related items). Am I missing something here for the databinding? XAML and XML below.

XAML:

<Window x:Class="MyTool.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="800">
    <Window.Resources>
        <XmlDataProvider x:Key="AllDeployments" XPath="Deployments" Source="Deployments.xml" />
        <DataTemplate x:Key="dtDeployments">
            <StackPanel FlowDirection="LeftToRight" Orientation="Horizontal">
                <TextBlock Text="{Binding XPath=@Name}" />
                <TextBlock Text=" - "/>
                <TextBlock Text="{Binding XPath=@Date}" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="dtFiles">
            <TextBlock Text="{Binding XPath=File}" />
        </DataTemplate>
    </Window.Resources>
    <Grid Name="gMain">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="2"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Menu Grid.Column="0" Grid.ColumnSpan="3"></Menu>
        <ListBox Grid.Column="0" Name="lbDeployment" 
                 ItemsSource="{Binding Source={StaticResource AllDeployments}, XPath=Deployment}"
                 ItemTemplate="{StaticResource dtDeployments}"></ListBox>
        <GridSplitter Grid.Column="1"></GridSplitter>
        <StackPanel Grid.Column="2">
            <ListBox Name="lbFiles" 
                     ItemsSource="{Binding Mode=TwoWay, ElementName=lbDeployments, Path=SelectedItem.InnerText, UpdateSourceTrigger=PropertyChanged}" 
                     ItemTemplate="{StaticResource dtFiles}"
                     Height="400"></ListBox>
        </StackPanel>
    </Grid>
</Window>

XML:

<?xml version="1.0" encoding="utf-8"?>
<Deployments MostRecentDate="12/31/2009 8:41:13 PM">
    <Filters>
     <Filter>.cs</Filter>
     <Filter>.csproj</Filter>
    </Filters>
    <Deployment Name="First Deployment" ID="1" Date="6/29/2009 8:41:13 PM">
     <File>file1.cs</File>
     <File>file2.cs</File>
    </Deployment>
    <Deployment Name="First Deployment" ID="1" Date="6/29/2009 8:41:13 PM">
     <File>file1.cs</File>
     <File>file2.cs</File>
    </Deployment>
    <Deployment Name="First Deployment" ID="1" Date="6/29/2009 8:41:13 PM">
     <File>file1.cs</File>
     <File>file2.cs</File>
    </Deployment>
    <Deployment Name="First Deployment" ID="1" Date="6/29/2009 8:41:13 PM">
     <File>file1.cs</File>
     <File>file2.cs</File>
    </Deployment>
    <Deployment Name="First Deployment" ID="1" Date="6/29/2009 8:41:13 PM">
     <File>file1.cs</File>
     <File>file2.cs</File>
    </Deployment>
</Deployments>
+3  A: 

Data bindings are tough because they tend to fail silently, which makes them very hard to debug. The number one issue that you had in your code was a misspelling of the ElementName on the lbFiles ListBox. Misspellings will get you every time! I was able to get the File names to appear after correcting the spelling mistake, removing the innerText portion of the binding, and removing the ItemTemplate. I'm not sure why the ItemTemplate wasn't working, but hopefully this example will get you moving again.

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="800">
<Window.Resources>
    <XmlDataProvider x:Key="AllDeployments" XPath="Deployments" Source="Deployments.xml" />
    <DataTemplate x:Key="dtDeployments">
        <StackPanel FlowDirection="LeftToRight" Orientation="Horizontal">
            <TextBlock Text="{Binding XPath=@Name}" />
            <TextBlock Text=" - "/>
            <TextBlock Text="{Binding XPath=@Date}" />
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="dtFiles">
        <TextBlock Text="{Binding XPath=File}" />
    </DataTemplate>
</Window.Resources>
<Grid Name="gMain">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="2"/>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Menu Grid.Column="0" Grid.ColumnSpan="3"></Menu>
    <ListBox Grid.Column="0" Name="lbDeployment" 
             ItemsSource="{Binding Source={StaticResource AllDeployments}, XPath=Deployment}"
             ItemTemplate="{StaticResource dtDeployments}"></ListBox>
    <GridSplitter Grid.Column="1"></GridSplitter>
    <StackPanel Grid.Column="2">
        <ListBox Name="lbFiles" 
                 ItemsSource="{Binding ElementName=lbDeployment, Path=SelectedItem, UpdateSourceTrigger=PropertyChanged}"                      
                 Height="400" />
    </StackPanel>
</Grid>

Tony Borres
Thanks for pointing out the misspelling. As for the template not working, I'm suspicious it has something to do with the XPath provided in it.
MasterMax1313