Link to home
Start Free TrialLog in
Avatar of MCKreed
MCKreed

asked on

Silverlight Listbox Binding

I'm trying to get a simple list of T to bind to a listbox. The listbox is binding but the text that I'm trying to display is not displaying. You can choose between items and if you debug with some extra code you can pull the data from the class object.
<UserControl x:Class="testlistbox.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid x:Name="LayoutRoot">
        <ListBox x:Name="PersonList" ItemsSource="{Binding ''}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding FirstName}" />
                        <TextBlock Text="{Binding LastName}" />

                    </StackPanel>
                </DataTemplate>

            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

namespace testlistbox
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            InitializeComponent();
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {


            PersonList.DataContext = MakePersonList();
            PersonList.SelectedIndex = -1;
           
        }
        private static List<Person> MakePersonList()
        {
            return (new List<Person>()
            {
               new Person() { FirstName = "John", LastName = "Smith" },
               new Person() { FirstName = "John", LastName = "Jones" }
            });

        }
      
    }
    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        
    }
}

Open in new window

Avatar of Ashok
Ashok
Flag of United States of America image

<UserControl x:Class="ListBoxFromClass.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
    xmlns:src="clr-namespace:ListBoxFromClass">
  <Grid x:Name="LayoutRoot">
      <Grid.Resources>
        <src:People x:Key="person"/>
      </Grid.Resources>
      <ListBox ItemsSource="{StaticResource person}" Width="350" Height="200" >
        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="{Binding FirstName}" />
              <TextBlock Text=", " />
              <TextBlock Text="{Binding LastName}" />
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
  </Grid>
</UserControl>

namespace ListBoxFromClass
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
       
    }

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Person(String firstName, String lastName)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
        }
    }

    public class People : List<Person>
    {
        public People()
        {
            Add(new Person("Michael", "Anderberg"));
            Add(new Person("Chris", "Ashton"));
        }
    }
}

HTH
Ashok
MCKreed,

I think you could make it work with your existing source code with little modifications.  I am also new to Silverlight and C#.
The problem was how the binding.  I would like to see it work with your code.  I spent two hours to figure out this.

Thanks,
Ashok
MCKreed,

Here is your style coding (without parametrized constructor) SHORT SYNTAX.....

namespace ListBoxFromClass
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class People : List<Person>
    {
       public People()
       {
           Add(new Person() { FirstName = "John", LastName = "Smith" });
           Add(new Person() { FirstName = "Chris", LastName = "Jones" });
       }
    }
}

HTH
Ashok
Avatar of MCKreed
MCKreed

ASKER

The 2 lines below, well the resource tag causes an expception.
Exception: XamlParseException
Message: "AG_E_PARSER_BAD_TYPE [Line: 12 Position: 36]"
StackTrace: "   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)\r\n   at testlistbox.MainPage.InitializeComponent()\r\n   at testlistbox.MainPage..ctor()"

I tried all the other mods, setting the itemsource or datacontext in the Page Loaded event. I get the same results, with the added comma. The list binds the data but will not display any text.


xmlns:src="clr-namespace:testlistbox"
 <src:People x:Key="p" />

Open in new window

Avatar of MCKreed

ASKER

Here's an image of what the binding with no text looks like.
listbox.jpg
ASKER CERTIFIED SOLUTION
Avatar of Ashok
Ashok
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
See attached image.
ListBox-Scr.jpg
Avatar of MCKreed

ASKER

I did the copy and paste thing with your code and it worked fine, so I decided to start from scratch and re-wrote the code. The second time it worked fine. I have gone back and forth trying to find the difference but cannot find anything.

Here are the 2 solutions if you want to take a look:

Working Solution
http://bolsterfarm.com/downloads/working.zip

Non-Working Solution
http://bolsterfarm.com/downloads/notworking.zip

I will check and let you know in a few days.

Thanks,
Ashok
Avatar of MCKreed

ASKER

I figured out what the problem was. The UI cannot access the person class if the class is not public.

Everything else was fine.
Oh, good.  That means I will not have to check.

Thanks,
Ashok