2012-01-03 39 views
0

做了一個簡單的測試項目中,我嘗試綁定到在原一的XmlDataSource視圖模型綁定到XMLDataProvider

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> 

然後,它的工作原理,如何綁定到視圖模型,所以我可以往裏面不只是在xmldatasource上

如果我把屬性上的斷點報告我可以看到它是calle當我做{綁定報告}但列表仍然是空的。

UPDATE

我能做到這一點的代碼綁定,然後它的作品

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

爲什麼不能我做,在XAML

+0

從您的代碼:'this.DataContext = _myViewModel.Person'什麼'Person'屬性?我沒有在你的視圖模型上看到它。你是說'_myViewModel.Reports'? – nemesv 2012-01-03 21:31:59

+0

對不起,將其更改爲Reports。這是在原代碼 – klashagelqvist 2012-01-04 09:44:46

+0

報告它可以與xmldatasource不是IEnumberable有關。但如果是這樣的話,當我設置datacontext它的作品,那麼必須有一些自動轉換在後臺進行 – klashagelqvist 2012-01-04 11:50:49

回答

0

好像我有一些問題,使用XPath的理解和我的一般問題是如何使用xaml綁定到視圖模型中的動態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); 
     } 
    } 
} 
+0

+1感謝您的更新,它有助於解決我的問題。 – 2012-11-16 15:35:28

相關問題