我有XML文檔
<?xml version="1.0" encoding="utf-8"?>
<Reports>
<Report Id="AAAAA-ABBB">
<DocId>10110001</DocId>
<DocName>ESP North Casing</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>10110002</DocId>
<DocName>ESP South Casing</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>
如果我定義靜態xmldataprovider像
<UserControl.Resources>
<XmlDataProvider x:Key="ReportData"
Source="../DesignData/report.xml"
XPath="Reports/Report" />
<DataTemplate x:Key="teamItemTemplate">
<Label Content="{Binding XPath=DocId}"/>
</DataTemplate>
</UserControl.Resources>
而且在列表框中顯示的文件
<ListBox x:Name="ReportListBox" Margin="60,12,114,64" DockPanel.Dock="Left"
ItemsSource="{Binding
Source={StaticResource ReportData}}"
ItemTemplate="{StaticResource teamItemTemplate}"
IsSynchronizedWithCurrentItem="True"
Visibility="Visible" SelectionMode="Single">
</ListBox>
我可以看到
,如果我做的代碼和負載xmldataprovider一樣在我的視圖模型
private XmlDataProvider GetXMLReports()
{
string filePath = Directory.GetCurrentDirectory() + @"\Data\report.xml";
XmlDataProvider provider = new XmlDataProvider();
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(filePath);
provider.Document = doc;
provider.XPath = "Reports/Report";
//Reports = _provider;
return provider;
}
public XmlDataProvider Reports
{
get { return _provider; }
set
{
_provider = value;
this.RaisePropertyChanged(() => this.Reports);
}
}
如果我設置的DataContext到視圖模型,並結合財產報告顯示
<ListBox x:Name="ReportListBox" Margin="60,12,114,64" DockPanel.Dock="Left"
ItemsSource="{Binding Path=Reports}"
ItemTemplate="{StaticResource teamItemTemplate}"
IsSynchronizedWithCurrentItem="True"
Visibility="Visible" SelectionMode="Single">
</ListBox>
沒有在列表框中的數據, 有任何想法嗎。
你是哪裏實際代碼調用bind()..? – MethodMan
我將usercontrol的datacontext綁定到viewmodel。如果我把一個斷點,我可以看到viewmodel加載。 – klashagelqvist
這是你的確切代碼嗎?我只問,因爲屬性分配/通知的東西似乎非常混亂(報告= _供應商是多餘的,例如,事件應在籌集者中提出) – GazTheDestroyer