0

I want to allow the user to choose an xml file (by clicking on a menu item) for it to then be processed using the xml data provider in codebehind if possible. How would I do this?

I can't bind on the source like this: <XmlDataProvider x:Key="ProductsXml" Source="{Binding OpenRecordMenuItem_Click}"/>

I know I could process the xml via a click handler or such on the menu item.

user3791372
  • 4,445
  • 6
  • 44
  • 78
  • I guess you should write at codebehind for bind a xml file to xmlDataprovider. "XmlDataProvider provider = new XmlDataProvider(); provider.Source = "Your uploaded file uri."; – Hiren Visavadiya Jan 13 '16 at 06:55

1 Answers1

0

This is the little bit sample, I hope this will be helpful to you. You can simply update the click event of button code with your menuitem click event.

MainWindow.

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="228,80,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    <Label Content="Label" Name="lblXmlFileName" HorizontalAlignment="Left" Margin="57,78,0,0" VerticalAlignment="Top" Width="144"/>
    <StackPanel x:Name="stackpanel1">
        <StackPanel.Resources>
        <XmlDataProvider x:Name="provider" x:Key="provider1">
            </XmlDataProvider>
        </StackPanel.Resources>
    </StackPanel>
</Grid>

Code Behind:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {   
// update validations as per your requirements...
            var dialog = new OpenFileDialog();
            dialog.Filter = "XML Files|*.xml";
            if (dialog.ShowDialog() == true)
            {   
                lblXmlFileName.Content = dialog.FileName.ToString();
                var provider1 = (XmlDataProvider) stackpanel1.Resources["provider1"];
                if(provider1 != null)
                 provider1.Source = new Uri(dialog.FileName, UriKind.Absolute);
            }
        }
    }
Hiren Visavadiya
  • 485
  • 1
  • 3
  • 15
  • how would i set this provider to one defined in the xaml? e.g. I have an xmldataprovider defined in a stackpanel's resources – user3791372 Jan 13 '16 at 07:30