-3

I have an xml file which contains multiple hierarchical data required for a WPF application. My objective is to first filter the data using the ComboBox and finally select the data inside the AttributeUp and AttributeDown tags into the code.

    <?xml version = "1.0" encoding="utf-8"?>
    <MenuItems xmlns="">
      <Menu Name="menu1">
        <SubMenu Name = "submenu1">
          <ItemsList>
            <Item Name = "item1">
              <AttributeUp>DataUp</AttributeUp>
              <AttributeDown>DataDown</AttributeDown>
            </Item>
         </SubMenu>
       </Menu>
   </MenuItems>

Below is the code to add XmlDataProvider to static resource.

<Window.Resources>
    <XmlDataProvider x:Key="updownItems" Source="./updownItemsList.xml" XPath="MenuItems/Menu" IsInitialLoadEnabled="True" IsAsynchronous="False"/>
</Window.Resources>

Below is how I am filtering the data

    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
             <Label Content="Level1" Width="35" Margin="0,0,0,0"></Label>
             <ComboBox x:Name="cmbBoxToplevel" IsEditable="True" SelectedIndex="0"  Width="110"
                     Margin="0,0,0,0" HorizontalAlignment="Right"
                     ItemsSource="{Binding Source={StaticResource updownItems}}" 
                     DisplayMemberPath="@Name" />
        </StackPanel>
                       
        <StackPanel Orientation="Horizontal">
             <Label Content="Level2"  Margin="0,0,0,0"></Label>
             <ComboBox x:Name="cmbBoxSecondLevel" IsEditable="True"  Width="110" SelectedIndex="0" 
                 Margin="0,0,0,0" HorizontalAlignment="Right"
                 DataContext="{Binding Path=SelectedItem, ElementName=cmbBoxToplevel}" 
                 ItemsSource="{Binding XPath=./Menu}" DisplayMemberPath="@Name" />
        </StackPanel>
        
       <StackPanel Orientation="Horizontal">
             <Label Content="Level3" Margin="0,10,0,0"></Label>
              <ComboBox x:Name="cmbBoxJanya" IsEditable="True"  Width="110" SelectedIndex="0" 
                   Margin="0,10,0,0" HorizontalAlignment="Right" DataContext="{Binding Path=SelectedItem, 
                   ElementName=cmbBoxSecondLevel}" ItemsSource="{Binding XPath=./ItemsList/Item}" 
                   DisplayMemberPath="@Name" />
         </StackPanel>
 </StackPanel>

With these, I am able to filter the xml tag names into different comboboxes. What I need to achieve is to access the contents of AttributeUp and AttributeDown directly into the C# code behind (MainWindow). I do not want to use any WPF elements to store these parameters. It it possible without writing a separate code for xml parsing.

Raghu
  • 114
  • 2
  • 10
  • Do you want [parse the XML document](https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c)? – vernou Feb 23 '21 at 08:32
  • I want to avoid XML parsing. As you can see, If I include a TextBlock, I can bind the xml data to it and access it from the code behind. I want to achieve similar thing by assigning the binding to a property. Is it possible. – Raghu Feb 23 '21 at 08:37

1 Answers1

1

You can bind AttributeUp and AttributeDown to the TextBlocks

<TextBlock x:Name="UpTxt" DataContext="{Binding ElementName=cmbBoxJanya, Path=SelectedItem}"
                   Text="{Binding XPath=./AttributeUp}"/>
<TextBlock x:Name="DownTxt" DataContext="{Binding ElementName=cmbBoxJanya, Path=SelectedItem}"
                   Text="{Binding XPath=./AttributeDown}"/>

And access values in the code behind

var upValue = UpTxt.Text;
var downValue = DownTxt.Text;
user2250152
  • 14,658
  • 4
  • 33
  • 57
  • Thanks, I was worried about TextBlock getting displayed. I used the Visibility="Hidden" property. I mark this as the answer with a recommendation to set Visibility to Hidden. – Raghu Feb 23 '21 at 09:06