绑定到XMLDataProvider

时间:2012-01-03 20:48:56

标签: wpf binding xmldatasource

做了一个简单的测试项目,我尝试绑定到proto viewmodel中的xmldatasource

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";
    }
}

如果我尝试绑定像这样的列表框。我什么都没得到

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

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>

然后它工作,我如何绑定到viewmodel所以我可以填充它不仅仅是在xmldatasource

如果我在属性上放置一个断点报告我可以看到当我执行{Binding Reports}时它被调用但是列表仍然是空的。

更新

我可以在代码中执行此绑定,然后它可以正常工作

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

为什么我不能在XAML中这样做

1 个答案:

答案 0 :(得分:0)

似乎我对XPath的理解存在一些问题,我的一般问题是如何使用xaml绑定到viewmodel中的动态xmldataprovider。像这样解决了。

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

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);
        }
    }
}