简体   繁体   中英

WPF - Bind a ListView to a .xml file dynamically

Is there a way (using the MVVM pattern) to dynamically bind a ListView to an xml file? Actually the ListView is binded to a static path, like:

C:\DocumentsAndSettings\blabla\morebla\log.xml 

I need something like

AppPath\log.xml

Here's my code: in the View.xaml:

<ListView ItemsSource="{Binding Source={StaticResource logDataSource}, ...

in the App.xaml:

        <XmlDataProvider x:Key="logDataSource" 
                     Source="C:\DocumentsAndSettings\blabla\morebla\log.xml" 
                     d:IsDataSource="True"/>

I'd like something like this:

        <XmlDataProvider x:Key="logDataSource" 
                     Source="AppPath\log.xml" 
                     d:IsDataSource="True"/>

Thank you in advance.

Why don't you just use relative paths? For example, if you put your log.xml in data directory in your project, simply write

<XmlDataProvider x:Key="logDataSource" Source="data/log.xml" />

Note that the file should have a build action of "resource" . Or if it's a "content" then set copy to o/p directory to "copy always" . (Search for difference b/w these two on Google)

If I put the log.xml file in my project directory, the logger library will write in the

project/bin/debug/log.xml

while the xaml binding will look for log.xml in

project/log.xml

I solved the problem setting the property of the log.xml file to "Content" not to "Resource"..even if I really don't know the difference :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM