如何使用c#和XAML进行DataBinding?

时间:2016-08-25 09:29:38

标签: c# wpf xaml

我正在尝试反序列化XML文件,并放置信息。在DataBingingList的帮助下进入XAML中的listBox。 我在Test.cs文件中有以下代码:

[XmlRoot("product")]
public class Test
{
    [XmlAttribute("id")]
    public string Id { get; set; }

    [XmlAttribute("categoryname")]
    public string CategoryName { get; set; }

    [XmlElement("name")]
    public string Name{get;set;}

    [XmlElement("price")]
    public Price price { get; set; }

    [XmlElement("description")]
    public description Description { get; set; }
    public override string ToString()
    {
        return this.Name + " " + price + " " + Description;
    }

    public BindingList<Test> all = new BindingList<Test>();
}
[XmlRoot("price")]
public class Price
{
    [XmlAttribute("unit")]
    public string Unit { get; set; }

    [XmlText]
    public int p { get; set; }

    public override string ToString()
    {
        return Unit + " " + p;
    }
}
[XmlRoot("description")]
public class description
{
    [XmlElement("color")]
    public string Color { get; set; }

    [XmlElement("size")]
    public string Size { get; set; }

    [XmlElement("weight")]
    public string Weight { get; set; }

    public override string ToString()
    {
        return Color + " " + " " + Size + " " + Weight;
    }

这是我的MainWindows.xaml.cs中的代码

    public MainWindow()
    {
         InitializeComponent();

        try
        {
            Test test = new Test();
            XmlSerializer deserializer = new XmlSerializer(typeof(Test));
            TextReader reader = new StreamReader(@"ajde.xml");
            test = (Test)deserializer.Deserialize(reader);
            reader.Close();
            ListBox1.ItemsSource = test.all; // ListBox from MainWindow.xaml
        }catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }`

}

在我的MainWindows.xaml中,我希望能够执行类似Value =“{Binding Path = Description}”的操作......

0 个答案:

没有答案