XmlDataProvider and static Xml resource


   
    

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Simple Window">

<Grid xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid.Resources>
    <XmlDataProvider x:Key="dataProvider" XPath="Employees">
      <x:XData>
        <Employees xmlns="">
          <Employee Type="Beginner">
            <YearOfWorking>1</YearOfWorking>
          </Employee>
          <Employee Type="Intermediate">
            <YearOfWorking>2</YearOfWorking>
          </Employee>
          <Employee Type="Advanced">
            <YearOfWorking>3</YearOfWorking>
          </Employee>
        </Employees>
      </x:XData>
    </XmlDataProvider>
  </Grid.Resources>
    <ListBox ItemsSource="{Binding Source={StaticResource dataProvider},XPath=Employee/YearOfWorking}" />
</Grid>
</Window>

   
    
    
    
     


Using XmlDataProvider


   
    

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Center" VerticalAlignment="Center">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
      <Grid.Resources>
        <XmlDataProvider x:Key="myXml" XPath="/Root">
          <x:XData>
            <Root xmlns="">
              <Item id="A" flag="True" value="A" />
              <Item id="B" flag="True" value="B" />
              <Item id="C" flag="False" value="C" />
              <Item id="D" flag="True" value="D" />
            </Root>
          </x:XData>
        </XmlDataProvider>
      </Grid.Resources>
      <ListView DataContext="{StaticResource myXml}" ItemsSource="{Binding XPath=Item}">
        <ListView.View>
          <GridView>
            <GridViewColumn Header="ID" DisplayMemberBinding="{Binding XPath=@id}" />
            <GridViewColumn Header="Enabled">
              <GridViewColumn.CellTemplate>
                <DataTemplate>
                  <CheckBox IsChecked="{Binding XPath=@flag}" />
                </DataTemplate>
              </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Value">
              <GridViewColumn.CellTemplate>
                <DataTemplate>
                  <TextBox Text="{Binding XPath=@value}" Width="70" />
                </DataTemplate>
              </GridViewColumn.CellTemplate>
            </GridViewColumn>
          </GridView>
        </ListView.View>
      </ListView>
    </Grid>
</Page>

   
    
    
    
     


Hierarchical Xml Templates


   
   
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Authors and Their Books">
    <Page.Resources>
        <XmlDataProvider x:Key="data" XPath="Authors">
            <x:XData>
                <Authors xmlns="">
                    <Author Name="Jane Austen">
                        <BirthDate>1972</BirthDate>
                        <DeathDate>2010</DeathDate>
                        <Books>
                            <Book Title="A">
                                <PubDate>2003</PubDate>
                            </Book>
                            <Book Title="B">
                                <PubDate>1813</PubDate>
                            </Book>
                        </Books>
                    </Author>
                </Authors>
            </x:XData>
        </XmlDataProvider>
        <HierarchicalDataTemplate DataType="Author" ItemsSource="{Binding XPath=Books/Book}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding XPath=@Name}" />
                <TextBlock Text=" (" />
                <TextBlock Text="{Binding XPath=BirthDate}" />
                <TextBlock Text="-" />
                <TextBlock Text="{Binding XPath=DeathDate}" />
                <TextBlock Text=")" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="Book">
            <StackPanel Orientation="Horizontal" TextBlock.FontSize="10pt">
                <TextBlock Text="{Binding XPath=@Title}" />
                <TextBlock Text="{Binding XPath=PubDate}" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </Page.Resources>
    <TreeView ItemsSource="{Binding Source={StaticResource data}, XPath=Author}" />
</Page>

   
    
    
     


Xmldata island.xaml


   
   

<Window x:Class="ControlDemos.xmldataisland"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ControlDemos" Height="300" Width="300">
  <Window.Resources>
    <XmlDataProvider x:Key="MyCompany" XPath="/Company/Country">
      <x:XData>
        <Company xmlns="">
          <Country Name="USA">
            <Employee>A</Employee>
          </Country>
          <Country Name="England">
            <Employee>B</Employee>
          </Country>
          <Country Name="Japan">
            <Employee>C</Employee>
          </Country>
        </Company>
      </x:XData>
    </XmlDataProvider>
  </Window.Resources>
  <Grid>
    <StackPanel>
      <ListBox>
        <ListBox.ItemsSource>
          <Binding Source="{StaticResource MyCompany}" XPath="/Company/Country"/>
        </ListBox.ItemsSource>
        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel>
              <TextBlock>
                <TextBlock.Text>
                  <Binding XPath="Employee"/>
                </TextBlock.Text>
              </TextBlock>
              <TextBlock>
                <TextBlock.Text>
                  <Binding XPath="@Name"/>
                </TextBlock.Text>
              </TextBlock>
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
    </StackPanel>
  </Grid>
</Window>

   
    
    
     


HierarchicalDataTemplate and XmlDataProvider


   
   

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TreeViewSelectedValue">
  <FrameworkElement.Resources>
    <XmlDataProvider x:Key="myCourseData" XPath="/CourseData">
      <x:XData>
        <CourseData xmlns="">
          <CourseInfo>
            <CourseName>A</CourseName>
            <CourseWorkDay>Monday</CourseWorkDay>
            <CourseWorkDay>Tuesday</CourseWorkDay>
            <CourseWorkDay>Wednesday</CourseWorkDay>
            <CourseWorkDay>Thrusday</CourseWorkDay>
            <CourseWorkDay>Friday</CourseWorkDay>
            <CourseStartTime>8:00am</CourseStartTime>
            <CourseNumber>12345</CourseNumber>
          </CourseInfo>
          <CourseInfo>
            <CourseName>B</CourseName>
            <CourseWorkDay>Monday</CourseWorkDay>
            <CourseWorkDay>Tuesday</CourseWorkDay>
            <CourseStartTime>6:30am</CourseStartTime>
            <CourseNumber>98765</CourseNumber>
          </CourseInfo>
        </CourseData>
      </x:XData>
    </XmlDataProvider>
    <HierarchicalDataTemplate DataType="CourseInfo" ItemsSource ="{Binding XPath=CourseWorkDay}">
      <TextBlock Text="{Binding XPath=CourseName}" />
    </HierarchicalDataTemplate>
  </FrameworkElement.Resources>
  <StackPanel>
    <TreeView ItemsSource="{Binding Source={StaticResource myCourseData}, XPath=CourseInfo}" 
        Name="myTreeView" SelectedValuePath="CourseNumber"/>

    <TextBlock Margin="10">SelectedValuePath: </TextBlock>
    <TextBlock Margin="10" Text="{Binding ElementName=myTreeView, Path=SelectedValuePath}"/>
    <TextBlock Margin="10">SelectedValue: </TextBlock>
    <TextBlock Margin="10" Text="{Binding ElementName=myTreeView, Path=SelectedValue}"/>

  </StackPanel>     

</Page>

   
    
    
     


Use XamlReader to read a Xaml string and convert to a Control


   
  

<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="DynamicXAML" Height="300" Width="300">
  <Grid Name="grid1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Window>
//File:Window.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Markup;
using System.Xml;
using System.IO;

namespace WpfApplication1
{

  public partial class Window1 : System.Windows.Window
  {

    public Window1()
    {
      InitializeComponent();

      StringReader sr = new StringReader(@"<Button xmlns=&#039;http://schemas.microsoft.com/winfx/2006/xaml/presentation&#039; 
        Foreground=&#039;BurlyWood&#039; FontSize=&#039;20pt&#039;>Click Me!</Button>");

      XmlReader reader = XmlReader.Create(sr);

      Button dynamicButton = (Button)XamlReader.Load(reader);

      this.grid1.Children.Add(dynamicButton);

      dynamicButton.Click += button1_Click;
   
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show("Dynamic Button Loaded From XAML String");
    }

  }
}

   
    
     


Use XamlReader to read Xaml xml file

   
  


<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="FlowDocReader Load/Save Sample" Width="640" Height="480">
  <StackPanel>
    <FlowDocumentReader Name="FlowDocRdr" Grid.Row="1"/>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using System.Windows.Markup;

namespace WpfApplication1
{

    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();

            FlowDocument content = null;
            
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "FlowDocument Files (*.xaml)|*.xaml|All Files (*.*)|*.*";

            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileStream xamlFile = openFile.OpenFile() as FileStream;
                if (xamlFile == null) return;
                else
                {   
                    try 
                    { 
                        content = XamlReader.Load(xamlFile) as FlowDocument; 
                        if (content == null) 
                            throw(new XamlParseException("The specified file could not be loaded as a FlowDocument."));
                    }
                    catch (XamlParseException e)
                    {
                        String error = "There was a problem parsing the specified file:

";
                        error += openFile.FileName;
                        error += "

Exception details:

";
                        error += e.Message;
                        System.Windows.MessageBox.Show(error);
                        return;
                    }
                    catch (Exception e) 
                    {
                        String error = "There was a problem loading the specified file:

";
                        error += openFile.FileName;
                        error += "

Exception details:

";
                        error += e.Message;
                        System.Windows.MessageBox.Show(error);
                        return;
                    }

                    FlowDocRdr.Document = content;
                }
            }
        }
    }
}