如何将xml绑定到mvvm wpf?

时间:2019-01-14 11:19:45

标签: c# xml wpf mvvm

我有问题。我有wpf mvvm应用程序,需要绑定xml,但是我不知道如何。

我有元素模型,元素vm和视图。一切正常,但是所有这些元素都具有“某些基础”模型。

class ItemModel
{
    public ItemModel(string name, double weight, double sg, double volume)
    {

        Name = name;
        Weight = weight;
        Sg = sg;
        Volume = volume;

    }

    public string Name { get; set; }
    public double Weight { get; set; }
    public double Sg { get; set; }
    public double Volume { get; set; }

}

这是我的虚拟机。

class ItemViewModel : BaseVM
{
    public ItemViewModel(string name, double sg, double weight,  double volume)
    {
        Name = name;
        Weight = weight;
        Sg = sg;
        Volume = volume;

    }

    public string Name { get; set; }

    private double _weight;
    public double Weight
    {
         get => _weight;
         set
         {
             _weight = value;

             RaisePropertyChanged();
         }
    }

    private double _sg;
    public double Sg
    {
        get => _sg;
        set
        {
            _sg = value;
            Weight = value * _volume;

            RaisePropertyChanged("Weight");
            RaisePropertyChanged("Sg");
        }
    }


    private double _volume;
    public double Volume
    {
        get => _volume;
        set
        {
            _volume = value;
            _weight = value * _sg;

            RaisePropertyChanged();
            RaisePropertyChanged("Weight");
            RaisePropertyChanged("Sg");
        }
    }
}

这是我的MainVM

class MainViewModel
{
    private DataModel Data;
    public ObservableCollection<ItemViewModel> Items { get; set; }

    public ListCollectionView FilteredItems { get; set; }


    public MainViewModel()
    {
        Data = new DataModel();
        Items = new ObservableCollection<ItemViewModel>();

    FilteredItems = new ListCollectionView(Items)
    {
        Filter = item => ((ItemViewModel)item).Volume != 0,
        IsLiveFiltering = true,
        LiveFilteringProperties =
        {
            nameof (ItemViewModel.Volume)
        }
    };

        Load();
    }

    public void Load()
    {
        foreach (var item in Data.GetItems())
            Items.Add(new ItemViewModel(item.Name, item.Weight, item.Sg, item.Volume));
    }
}

我有一些“ DataModel”

class DataModel
{
    public List<ItemModel> GetItems() =>
        new List<ItemModel>
        {
            new ItemModel("Water", 0.00, 1.025, 0.00),
            new ItemModel("Ballast", 0.00, 1.000, 0.00),
            new ItemModel("Oil", 0.00, 1.040, 0.00),
        };
}

这是我想绑定的xml。

<ballast>
  <tank ID="FPTW" Name="Forepeak" Weight="0.00" SG="1.025" Volume="0.00"> </tank>
</ballast>

请帮助我如何绑定此xml文件,而不是在DataModel中列出。

1 个答案:

答案 0 :(得分:0)

我强烈建议您将XML反序列化为“真实”对象。在您的示例中,您希望在程序中有一个坦克清单。视图(XAML)不应该知道“数据存储”是xml文件,数据库还是其他。只需绑定到TankViewModel的列表上即可(或者您将其命名为ItemViewModel。希望您不要在所有这些战车上都炸毁了东西。...

这是将xml反序列化为“真实”对象的方式(完整的工作控制台应用程序:

https://dotnetfiddle.net/zCHSmc

using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {

        MainViewModel vm = new MainViewModel();
        foreach(tank t in vm.ListOfTanks.ballast)
        {
            Console.WriteLine(t.ID);
        }


    }

    public class MainViewModel
    {
        public tanks ListOfTanks
        {
            get
            {
                string xmlContent = "<?xml version=\"1.0\" encoding=\"utf-16\"?><tanks xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">  <ballast><tank><ID>ksj</ID></tank>  </ballast></tanks>";
                tanks allTanks = null;

                XmlSerializer serializer = new XmlSerializer(typeof(tanks));

                //if you have a xml file you can use serializer.Deserialize("yourFile.xml"); instead of the "in memory stream"

                using (TextReader reader = new StringReader(xmlContent))
                {
                    allTanks =(tanks)serializer.Deserialize(reader);
                }
                return allTanks;
            }
        }
    }


    public class tanks
    {
        public tanks()
        {
            ballast = new List<tank>();
        }
        public List<tank> ballast {get;set;} 
    }


    public class tank
    {
        public string ID {get;set;}
    }
}