Results 1 to 4 of 4

Thread: Binding listbox to XML - accessing node value

  1. #1

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    40

    Binding listbox to XML - accessing node value

    I have a listbox which uses an XMLDataProvider as datacontext and is bound to an XPath query which returns a number of nodes with different names.
    These names will vary at runtime therefore are unknown at design time and must be accessed dynamically.
    I am trying to set the Item template to display both the name of the node and the value it holds. I can get the name through {Binding Name} but can't seem to get the value (I already tried {Binding Value}) - can anyone help?

    XAML:
    Code:
    <ListBox Margin="2" ItemsSource="{Binding XPath=variables/*}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Margin="2" Text="{Binding Name}"></TextBlock>
                    <TextBlock Margin="2" Text="{WHAT GOES HERE?}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    Example XPATH result:
    Code:
    <variables>
         <SourceFolder>[WORKROOT]\startfolder</SourceFolder>
         <TargetFolder>[WORKROOT]\endfolder</TargetFolder>
         <ReadOnly>True</ReadOnly>
    </variables>
    So with the example I'm looking for the listbox to show 3 lines:
    SourceFolder [WORKROOT]\startfolder
    TargetFolder [WORKROOT]\endfolder
    ReadOnly True

  2. #2
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: Binding listbox to XML - accessing node value

    I believe you use an XPath query in the binding for the textblock text property as well.

    Have a look at http://blogs.msdn.com/ashish/archive...19/762085.aspx

    I think it would look something like this

    Code:
    <ListBox DataContext="{StaticResource AshishBlog}"  ItemsSource="{Binding XPath=item/title}"/>
    
    </Grid>
    Hope that points you in the right direction.

    Thanks
    Anti DUPLO machine!!!

  3. #3
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: Binding listbox to XML - accessing node value

    Here is a better example, This is complete working code... You can paste this into a new window and run it as it, no code behind needed.

    Code:
    <Window.Resources>
            <XmlDataProvider x:Key="DrWPFFeeds" Source="http://www.drwpf.com/blog/Home/tabid/36/rssid/1/Default.aspx"/>
            <DataTemplate x:Key="CustomTemplate">
               <Border BorderThickness="1" BorderBrush="Cyan" Width="Auto">
                <StackPanel Margin="1">
                    <TextBlock Text="{Binding XPath=title}"  Foreground="#515151" />
                    <TextBlock Text="{Binding XPath=author}"  Foreground="#515151" />
                    <TextBlock Text="{Binding XPath=pubDate}"  Foreground="#515151" />
                </StackPanel>
               </Border>
            </DataTemplate>
        </Window.Resources>
        <Grid DataContext="{Binding Source={StaticResource DrWPFFeeds}, XPath=/rss/channel/item}">
            <ListBox  Name="ListBox1" ItemsSource="{Binding}"  Grid.Column="0" Grid.Row="1"
                      ItemTemplate="{StaticResource CustomTemplate}" Margin="0,0,0,30">
            </ListBox>
        </Grid>
    You can see how I am using the XPath in when I set the DataContext in the grid, then all I need to do is say, title, author, etc.

    Hopefully this was more helpful.

    Thanks
    Anti DUPLO machine!!!

  4. #4

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    40

    Re: Binding listbox to XML - accessing node value

    Sorry, I think I may have been a bit unclear - the listbox is actually nested in another datatemplate and the problem I have is that the XMl returned by {Binding XPath=variables/*} will vary so I can't access the childnodes by name as I don't know at design-time what the names will be.
    I need to be able to access both the name of the node and the value of the data it holds without using the actual names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width