WPF XML DataBinding

时间:2009-06-30 01:38:30

标签: wpf xml data-binding

我正在尝试做一些WPF数据绑定,但此刻我有点挂了。我有两个列表框和一个XML文件。第一个列表框成功绑定到XML源。但是,当我尝试从第一个列表框绑定到所选项目的子项作为第二个列表框的源时,不会出现任何内容。目标是索引或查找(选择一个索引导致查找相关项目)。我在这里缺少数据绑定的东西吗?下面是XAML和XML。

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>

1 个答案:

答案 0 :(得分:3)

数据绑定很难,因为它们往往会无声地失败,这使得它们很难调试。您在代码中遇到的首要问题是lbFiles ListBox上的ElementName拼写错误。拼写错误每次都会帮到你!在纠正拼写错误,删除绑定的innerText部分并删除ItemTemplate后,我能够显示文件名。我不确定为什么ItemTemplate不起作用,但希望这个例子能让你再次活动。

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