locked
Bind xmldataprovider using MVVM pattern RRS feed

  • Question

  • I have a view model in wpf application, which requires high usage of XML. For this I am using XMLDataProvider also there are some other properties that are not dependent upon the XML. 

    So my viewmodel looks like this:

            public class ViewModel : ViewModelBase
            {
                private XmlDocument _xmlDataProvider;
                private string _name;
                public string Name
                {
                    get { return _name; }
                    set
                    {
                        _name = value;
                        base.RaisePropertyChangedEvent("Name");
                    }
                }
                public XmlDocument XmlDataProvider
                {
                    get { return _xmlDataProvider; }
                    set
                    {
                        _xmlDataProvider = value;
                        base.RaisePropertyChangedEvent("XmlDataProvider");
                    }
                }
            }

    And my XAML code Looks like this:

        <UserControl x:Name="ctrlTemplate" x:Class= "CtrlTemplate"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                     xmlns:local="clr-namespace:WPFControl.UI"
                     xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
                     xmlns:xckt="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
                     mc:Ignorable="d" 
                     DataContext="{DynamicResource ViewModel}">
            <UserControl.Resources>
                    <local:ViewModel x:Key="ViewModel" />
                </ResourceDictionary>
            </UserControl.Resources>

    I want to set the xml provider to an internal grid Like this:

            <Grid x:Name="grdFill" Margin="0" RenderTransformOrigin="0.502,0.492" HorizontalAlignment="Left">
                    <Grid.DataContext>
                        <XmlDataProvider x:Name="template" Document="{Binding XmlDataProvider}"/> **This is not possible as Document is not a Dependency property**
                    </Grid.DataContext>
                    <StackPanel>
                        <Button ="{Binding XPath=Some Xpath}"/>
                        <Button Content="{Binding Name, Source={StaticResource ViewModel}}"/>
                    </StackPanel>
            </Grid>

    So my question is:

    1. How can I set the **Document** property of XmlDataProvider, without violating MVVM rules?

    2. I also am using commands in place of events, but The **Document** property would not be available in the ViewModel.
    Friday, October 9, 2015 12:38 PM

Answers

  • You don't need an XmlDataProvider. Just set the DataContext of the Grid to your XmlDocument property:

            <Grid x:Name="grdFill" Margin="0" RenderTransformOrigin="0.502,0.492" HorizontalAlignment="Left"
                  DataContext="{Binding XmlDataProvider}">
                <StackPanel>
                    <TextBlock Text="{Binding XPath=Some Xpath}"/>
                </StackPanel>
            </Grid>

    Or add the XmlDataProvider itself to the view model: http://stackoverflow.com/questions/8718403/binding-to-xmldataprovider

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Friday, October 9, 2015 12:53 PM

All replies

  • 1) You can't really.

    The purpose of the viewmodel is to provide the data (model) to the view.

    You're providing the data from the view rather than viewmodel.

    I very very rarely use any of the various dataproviders.

    Basically your xpath probably ought to be in the viewmodel and that should provide objects to the view to bind.

    Unless you're doing something quite strange.


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    Friday, October 9, 2015 12:45 PM
  • You don't need an XmlDataProvider. Just set the DataContext of the Grid to your XmlDocument property:

            <Grid x:Name="grdFill" Margin="0" RenderTransformOrigin="0.502,0.492" HorizontalAlignment="Left"
                  DataContext="{Binding XmlDataProvider}">
                <StackPanel>
                    <TextBlock Text="{Binding XPath=Some Xpath}"/>
                </StackPanel>
            </Grid>

    Or add the XmlDataProvider itself to the view model: http://stackoverflow.com/questions/8718403/binding-to-xmldataprovider

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Friday, October 9, 2015 12:53 PM