简体   繁体   English

如何在运行时更改wpf中的xmldataprovider源?

[英]how to change xmldataprovider source in wpf at runtime?

I made an application which draws a tree of organization based on values from an xml file. 我创建了一个应用程序,它根据xml文件中的值绘制组织树。

The xaml file goes like this : xaml文件是这样的:

<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? 我希望能够通过从openfiledialog中选择一个xml文件(如按钮点击)在运行时更改源代码,我该怎么做?

You can get the XmlDataProvider instance by writing (XmlDataProvider)this.Resources["organization"] in the code file. 您可以通过在代码文件中编写(XmlDataProvider)this.Resources["organization"]来获取XmlDataProvider实例。

You can then set the Source property to a path from a file dialog. 然后,您可以将Source属性设置为文件对话框中的路径。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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