绑定到XMLDataProvider

绑定到XMLDataProvider

问题描述:

做了一个简单的测试项目中,我试图绑定到的XmlDataSource在原视图模型

Made a simple test project where i try to bind to a xmldatasource in a proto viewmodel

public partial class Window1 : Window
{
    //private XmlDataProvider _provider = new XmlDataProvider(); 
    private MyViewModel _myViewModel = new MyViewModel();
    public Window1()
    {
        InitializeComponent();
        this.DataContext = _myViewModel ;
    }    
}

public class MyViewModel
{    
    public MyViewModel()
    {
        LoadXMLData();
    }

    private XmlDataProvider _provider = new XmlDataProvider(); 
    public XmlDataProvider Reports
    {
        get { return _provider; }
        set { _provider = value; }
    }    

    private void LoadXMLData()
    {
        string filePath = Directory.GetCurrentDirectory() + @"\Reports2.xml";

        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        doc.Load(filePath);
        _provider.Document = doc;
        _provider.XPath = @"Reports/Report";
    }
}

如果我尝试绑定这样一个列表框。我什么也没得到。

If i try to bind a listbox like this. I get nothing

<ListBox x:Name="TeamsListBox" Margin="0,0,0,20" DockPanel.Dock="Left"
    ItemsSource="{Binding Reports}"
    ItemTemplate="{StaticResource teamItemTemplate}"
    IsSynchronizedWithCurrentItem="True"
    Visibility="Visible" SelectionMode="Single">
</ListBox>

如果我不是改变的DataContext为

If i instead change datacontext to

this.DataContext = _myViewModel.Reports

和列表框

<ListBox x:Name="TeamsListBox" Margin="0,0,0,20" DockPanel.Dock="Left"
    ItemsSource="{Binding}"
    ItemTemplate="{StaticResource teamItemTemplate}"
    IsSynchronizedWithCurrentItem="True"
    Visibility="Visible" SelectionMode="Single">
</ListBox>

然后,它的工作原理,我怎么绑定到视图模型,所以我可以用比刚上的XmlDataSource

Then it works, how do i bind to the viewmodel so i can fill it with more than just on xmldatasource

如果我把一个断点物业报告中,我可以看到,它被称为当我做{结合报告}但名单仍是空的。

If i put a breakpoint on property Report i can see that it is called when i do {Binding Reports} but the list is still empty.

更新

我能做到这一点在code绑定,然后它的作品

I can do this binding in code and then it works

 Binding binding = new Binding();
            binding.Source = _myViewModel.Reports;
            binding.XPath = @"Reports/Report";
            TeamsListBox.SetBinding(ListBox.ItemsSourceProperty, binding);

为什么不能我这样做在XAML

Why cant i do that in XAML

好像我有一些问题,使用XPath的理解和我一般的问题是如何绑定到一个动态xmldataprovider与XAML中的视图模型。解决了这个样子。

Seems like i had some problems with the understanding of XPath and my general question was how to bind to a dynamic xmldataprovider in a viewmodel with xaml. Solved it like this.

XML

<?xml version="1.0" encoding="utf-8"?>
<Reports xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Report Id="AAAAA-ABBB">
    <DocId>30110001</DocId>
    <DocName>Report name1</DocName>
    <DocType>2010-01-01</DocType>
    <Status>1</Status>
    <CreatedById>1</CreatedById>
    <SiteId>1</SiteId>
    <Language>1</Language>
    <Updated>2011-01-01</Updated>
    <Published>2011-01-01</Published>
    <FilePath>c:\\reports\20011001.docx</FilePath>
  </Report>
  <Report Id="AAAAA-ABBC">
    <DocId>30110002</DocId>
    <DocName>Report name2</DocName>
    <DocType>2010-01-01</DocType>
    <Status>1</Status>
    <CreatedById>1</CreatedById>
    <SiteId>1</SiteId>
    <Language>1</Language>
    <Updated>2011-01-01</Updated>
    <Published>2011-01-01</Published>
    <FilePath>c:\\reports\20011001.docx</FilePath>
  </Report>
</Reports>

窗口1

    <Window x:Class="WpfApplication2.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
              Title="Window1" Height="300" Width="300">
        <Window.Resources>

            <DataTemplate x:Key="reportItemTemplate">
                <StackPanel Orientation="Horizontal">
                <Label Content="{Binding XPath=DocId}"/>
                    <Label Content="{Binding XPath=DocName}"/>
                </StackPanel>
            </DataTemplate>
        </Window.Resources>
        <StackPanel  DataContext="{Binding LocalReports}" >
            <ListBox  

            ItemsSource="{Binding}"
                ItemTemplate="{StaticResource reportItemTemplate}"
                         IsSynchronizedWithCurrentItem="True"
                         Visibility="Visible" SelectionMode="Single"
                />
            <TextBox Text="{Binding XPath=DocId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <TextBox Text="{Binding XPath=DocName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <Button Content="Button" Height="23" Name="button1" Click="button1_Click" />
        </StackPanel>

    </Window> 

Window1.xaml.cs

Window1.xaml.cs

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        //private XmlDataProvider _provider = new XmlDataProvider(); 
        private MyViewModel _myViewModel = new MyViewModel();

        public Window1()
        {
            InitializeComponent();

            this.DataContext = _myViewModel;


        }


         private void button1_Click(object sender, RoutedEventArgs e)
         {
             _myViewModel.Save();
         }

    }

    public class MyViewModel
    {

        public MyViewModel()
        {
                      }

        private XmlDataProvider _provider;
        public XmlDataProvider LocalReports
        {

            get
            {
                String file = Directory.GetCurrentDirectory() + @"\Reports2.xml";
                _provider = new XmlDataProvider()
                {
                    Source = new Uri(file, UriKind.Absolute),
                    XPath = "Reports/Report"
                };
                return _provider;
            }
        }



        }


        public  void Save()
        {
            string source = _provider.Source.LocalPath;
            _provider.Document.Save(source);
        }
    }
}