How to change xmldataprovider source in wpf at runtime?

I made an application that draws an organization tree based on values ​​from an XML file.

The xaml file is as follows:

<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 code at runtime by selecting an xml file from openfiledialog (like clicking a button), how to do this?

+3
source share
1 answer

You can get an instance XmlDataProviderby writing (XmlDataProvider)this.Resources["organization"]in a code file.

Then you can set the property Sourceto the path from the file dialog box.

For instance:

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);
+6
source

Source: https://habr.com/ru/post/1727965/


All Articles