数据绑定停止工作,不知道为什么

时间:2012-03-04 01:26:59

标签: c# wpf data-binding

我一直在研究WPF应用程序,我正在绑定数据,突然没有出现图像,数据通过XML阅读器从XML文件解析,其余数据解析得很好,但对于某些因为我在按钮上使用的图像不会出现。这是我在xmal的代码和一个有这个问题的区域的c#文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Xml;
using System.Drawing;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        XDocument xd = new XDocument();


        LoadImages();
    }


    void LoadImages()
    {
        List<ImageScroller> album = new List<ImageScroller>();
        XmlReader r = XmlReader.Create("galleries.xml");

        while (r.Read())
        {
            string u, n, f;
            if (r.NodeType == XmlNodeType.Element && r.Name == "name")
            {
                r.Read();
                n = r.Value;
                r.Read();
                r.Read();
                r.Read();
                r.Read();
                u = r.Value;
                r.Read();
                r.Read();
                r.Read();
                r.Read();
                f = r.Value;
                album.Add(new ImageScroller { url = u, title = n, filename = f });

            }

        }

        viewGalleries.ItemsSource = album;
    }

    private void pButton_Click(object sender, RoutedEventArgs e)
    {


    }
}

public class ImageScroller
{
    public string url { get; set; }
    public string title { get; set; }
    public string filename { get; set; }
}

}

这是我的xaml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid x:Name="LayoutRoot" Background="LightGreen">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="50"></RowDefinition>
    </Grid.RowDefinitions>
    <ListBox Name="viewGalleries" Grid.Row="0" Background="LightGreen">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="Layout" Background="LightGreen">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="50"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Button x:Name="pButton" Width="400" Height="400" Tag="{Binding Path=filename}" Click="pButton_Click">
                        <Image Width="400" Height="400" Name="imgSource" 
                               VerticalAlignment="Center" HorizontalAlignment="Center" 
                                Source="{Binding Path=url}" Grid.Row="0"/>
                    </Button>
                    <TextBlock FontSize="32" Grid.Row="1" Name="TextSource" 
                           VerticalAlignment="Center" HorizontalAlignment="Center" 
                           Text="{Binding Path=title}"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <TextBlock Name="test" Grid.Row="1"></TextBlock>
</Grid>
</Window>

请有人请,请告诉我为什么这不起作用?它只是按钮中没有绑定的图像,它工作正常,它突然停止了。我不知道是否我不小心删除了一个引用或者什么,但是如果我不明白的话,我就这么搞砸了。

0 个答案:

没有答案