none
How to assign Listbox name and Uri source to the listboxitems from xml

    Question

  • The idea is to have a listbox which will be generated from xml. Each item will have "name" and "UriLink". I need to navigate to the pages on click on the listboxitem. How can I populate name and UriLink using xml and assign it to listbox items. I was experimented with it but it seems like I am missing something. Thank you in advance.
    Monday, August 23, 2010 3:35 PM

Answers

  • You can take the XML and Convert into the list<obj> and you can assign it to the ComboBox.

     

    Please let me know if you need more help.

    Monday, August 23, 2010 5:56 PM
  • Insted of binding the PageData to ListBox Bind the List<Object>

     

    i mean  As shown Below

     

    Create a private Class in the respective VM

     

    private class pageInfo

    {

    public string name

    {

    get;

    set;

    }

    public String source

    {

    get;

    set;

    }

    }

     

    create a property 

     

    public ilist<pageInfo> PageInformation

    {

    get;

    set;

    }

     

    convert the XML to the PageInformation object  using XMl Serialization it's just regular way.

     

    public PageInfo _pageInfo;

    public PageInfo SeledtedPageinfo

    {

    get

    {

    return _pageInfo;

    }

    set

    {

    _pageInfo = value;

    }

    once you converted to the object 

     

    <ListBox x:Name="Nav_ListBox" Margin="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
    					Background="#FFF2F2F2" ItemTemplate="{DynamicResource pageTemplate}" 
    					ItemsSource="{Binding path=PageInformation }" 
    					selectedItem= {Binding path=SeledtedPageinfo}
    					Padding="6" IsTabStop="True" BorderBrush="Black" 
    					FontFamily="Segoe UI Light" Foreground="Black" 
    					BorderThickness="0" Width="200" SelectedIndex="1" Style="{DynamicResource ListBoxStyle1}"
    					Grid.Column="0" HorizontalAlignment="Left" SelectionChanged="SelectionChanged" 
    					

    Now the SelectedPageInfo has the Selcet value 

    selectedpageinfo.Name, selctedPageinfo.source you can get.

     

    Please let me know if you need any more help.

    Monday, August 23, 2010 6:59 PM
  • It's IList

     

    IList require name space using System.Collections.Generic;

     

    Please let me know if you still face the problem

    • Marked as answer by vladc77 Thursday, August 26, 2010 4:33 PM
    Monday, August 23, 2010 11:53 PM
  • Thank you. I will try this technique. However, I'd like to let you know that I was able to make it purely in XAML without any C# code. I databinded Frame datacontext with listbox item selected in and then for Frame source defined XPath source. It works perfect. Nevertheless, I want to employ technique using C# as well.
    Tuesday, August 24, 2010 3:17 PM
  • Hi Vladc77,

    If you are doing a Two-way binding to a large amount of xml data, you might want to try Reddy's suggestion since XML binding is relatively slow. Otherwise you should just stay with your current approach, it might be more efficient than doing convertion.

    Hope this helps.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, August 26, 2010 2:25 AM
    Moderator
  • Thank you for you comments. It is very helpful for me to know.
    • Marked as answer by vladc77 Thursday, August 26, 2010 4:33 PM
    Thursday, August 26, 2010 4:30 PM

All replies

  • i Have The same kind of scenario but i am using combobox based on the selecteditem of the combo box i open the uri in the browser.

     

     System.Diagnostics.Process.Start(SelectedItem.Uri);

    Monday, August 23, 2010 4:01 PM
  • Do you populate comboboxitem names and Uri links from XML? How do you pass it on click? Thank you!
    Monday, August 23, 2010 5:00 PM
  • You can take the XML and Convert into the list<obj> and you can assign it to the ComboBox.

     

    Please let me know if you need more help.

    Monday, August 23, 2010 5:56 PM
  • I am wondering if you can send me a simple example. I am employing XmlDataProvider

    <XmlDataProvider x:Key="PagesData" XPath="Pages" Source="Data/NavList_DataSource.xml" />
    
    		<DataTemplate x:Key="pageTemplate">
    			<StackPanel Orientation="Horizontal" >
    				<Image Source="Resources/Images/icon_placeholder_dark20x20.png" Margin="4" VerticalAlignment="Center" />
    				<TextBlock Text="{Binding XPath=name}" FontFamily="Segoe UI" LineHeight="22" Foreground="#FF000000" FontSize="14" VerticalAlignment="Center" Margin="4" />
    			</StackPanel>
    		</DataTemplate>

    Assigning it to the ListBox

    <ListBox x:Name="Nav_ListBox" Margin="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
    					Background="#FFF2F2F2" ItemTemplate="{DynamicResource pageTemplate}" 
    					ItemsSource="{Binding Source={StaticResource PagesData}, XPath=page}" 
    					Padding="6" IsTabStop="True" BorderBrush="Black" 
    					FontFamily="Segoe UI Light" Foreground="Black" 
    					BorderThickness="0" Width="200" SelectedIndex="1" Style="{DynamicResource ListBoxStyle1}"
    					Grid.Column="0" HorizontalAlignment="Left" SelectionChanged="SelectionChanged" 
    					
    
    					/>
    XML
    <Pages xmlns="">
    	<page id="page01">
    		<name>Page 1</name>
    		<source>Pages/Page1.xaml</source>
    	</page>
    	<page id="page02">
    		<name>Page 2</name>
    		<source>Pages/Page2.xaml</source>
    	</page>
    </Pages>

    It populates the listbox with all items. Then I need to load the pages in the Frame on ListboxItem Selecxted or clicked:

    Xaml for Frame:

    <Frame x:Name="ContentFrame" Source="{Binding ElementName=Nav_ListBox, Path=SelectedItem.Source}" JournalOwnership="OwnsJournal" NavigationUIVisibility="Hidden" Grid.Column="2" HorizontalAlignment="Left" Width="Auto">
    				</Frame>
    I need to pass the Uri value on item selected or click. I am still looking for the way to make it right. Any ideas?
    Monday, August 23, 2010 6:22 PM
  • Insted of binding the PageData to ListBox Bind the List<Object>

     

    i mean  As shown Below

     

    Create a private Class in the respective VM

     

    private class pageInfo

    {

    public string name

    {

    get;

    set;

    }

    public String source

    {

    get;

    set;

    }

    }

     

    create a property 

     

    public ilist<pageInfo> PageInformation

    {

    get;

    set;

    }

     

    convert the XML to the PageInformation object  using XMl Serialization it's just regular way.

     

    public PageInfo _pageInfo;

    public PageInfo SeledtedPageinfo

    {

    get

    {

    return _pageInfo;

    }

    set

    {

    _pageInfo = value;

    }

    once you converted to the object 

     

    <ListBox x:Name="Nav_ListBox" Margin="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
    					Background="#FFF2F2F2" ItemTemplate="{DynamicResource pageTemplate}" 
    					ItemsSource="{Binding path=PageInformation }" 
    					selectedItem= {Binding path=SeledtedPageinfo}
    					Padding="6" IsTabStop="True" BorderBrush="Black" 
    					FontFamily="Segoe UI Light" Foreground="Black" 
    					BorderThickness="0" Width="200" SelectedIndex="1" Style="{DynamicResource ListBoxStyle1}"
    					Grid.Column="0" HorizontalAlignment="Left" SelectionChanged="SelectionChanged" 
    					

    Now the SelectedPageInfo has the Selcet value 

    selectedpageinfo.Name, selctedPageinfo.source you can get.

     

    Please let me know if you need any more help.

    Monday, August 23, 2010 6:59 PM
  • Unfortunatelly, I cannot make it work. I am missing something.

    ilist name gets errors in VS. I cannot go further to experiment. Do you have any idea why it happens this way?

    public ilist<pageInfo> PageInformation
    {
    get;
    set;
    }
    
    I also don't see how I can update Frame source value from here. I am wondering if you have any working sample or I can provide a project files if it will be easier. Thank you again.
    Monday, August 23, 2010 11:16 PM
  • It's IList

     

    IList require name space using System.Collections.Generic;

     

    Please let me know if you still face the problem

    • Marked as answer by vladc77 Thursday, August 26, 2010 4:33 PM
    Monday, August 23, 2010 11:53 PM
  • Thank you. I will try this technique. However, I'd like to let you know that I was able to make it purely in XAML without any C# code. I databinded Frame datacontext with listbox item selected in and then for Frame source defined XPath source. It works perfect. Nevertheless, I want to employ technique using C# as well.
    Tuesday, August 24, 2010 3:17 PM
  • Hi Vladc77,

    If you are doing a Two-way binding to a large amount of xml data, you might want to try Reddy's suggestion since XML binding is relatively slow. Otherwise you should just stay with your current approach, it might be more efficient than doing convertion.

    Hope this helps.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, August 26, 2010 2:25 AM
    Moderator
  • Thank you for you comments. It is very helpful for me to know.
    • Marked as answer by vladc77 Thursday, August 26, 2010 4:33 PM
    Thursday, August 26, 2010 4:30 PM