how to change xmldataprovider source in wpf at runtime?

本秂侑毒 提交于 2019-12-07 16:25:43

问题


I made an application which draws a tree of organization based on values from an xml file.

The xaml file goes like this :

<Window.Resources>


    <!-- The Org Chart Data-->
    <XmlDataProvider x:Key="organization"  Source="model.xml" />

    <SolidColorBrush x:Key="ListBorder" Color="#FF7F9DB9"/>

    <!-- The Style for Nodes -->
    <Style TargetType="{x:Type draw:Node}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Template">
        ---------------------------------------------------------

I want to be able to change the source at runtime by selecting a xml file from openfiledialog (like button click) how do i do that?


回答1:


You can get the XmlDataProvider instance by writing (XmlDataProvider)this.Resources["organization"] in the code file.

You can then set the Source property to a path from a file dialog.

For example:

var provider = (XmlDataProvider)this.Resources["organization"];
var dialog = new OpenFileDialog();
dialog.Filter = "XML Files|*.xml";
if (dialog.ShowDialog(this)) {
    provider.Source = new Uri(dialog.FileName, UriKind.Absolute);


来源:https://stackoverflow.com/questions/2037906/how-to-change-xmldataprovider-source-in-wpf-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!