none
Use of ObjectDataProvider ?

    Question

  • Hi,
    I want to use the ObjectDataProvider provider in a ListBox. I want to display informations from my C# class in a listbox.

    <Window x:Class="DataBindingProof.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:DataBindingProof"
        Title="DataBindingProof" Height="700" Width="700"
        >
        <StackPanel Orientation="Vertical">
            <StackPanel.Resources>
                <ObjectDataProvider ObjectType="{x:Type m:MyStrings}" x:Key="MyStringData"/>
            </StackPanel.Resources>

            <ListBox Name="theListBox" Width="200" Height="300" ItemsSource="{Binding Source={StaticResource MyStringData}}"/>

        </StackPanel>
    </Window>

    And I have this:
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace DataBindingProof
    {
        public class MyStrings : List<String>
        {
            public MyStrings()
            {
                this.Add("Hello");
                this.Add("Goodbye");
                this.Add("Heya");
                this.Add("Cya");
            }
        }
    }

    But it doesn't work !!
    What is my mistake ?

    Thanks for your help.

    Olivier
    Wednesday, May 10, 2006 8:45 AM

Answers

  • Indeed, in the code I've provided, maybe a converter is needed.

    But, I'm also test your code and this one run fine on my computer...

    Why did you say that it doesn't work ?
    Wednesday, May 10, 2006 5:31 PM

All replies

  • Try something like this:

    Hi,
    I want to use the ObjectDataProvider provider in a ListBox. I want to display informations from my C# class in a listbox.

    <Window x:Class="DataBindingProof.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:DataBindingProof"
        Title="DataBindingProof" Height="700" Width="700"
        >
        <StackPanel Orientation="Vertical">
            <StackPanel.Resources>
                <ObjectDataProvider ObjectType="{x:Type m:MyStrings}" MethodName="DisplayInfo" x:Key="MyStringData"/>
            </StackPanel.Resources>

            <ListBox Name="theListBox" Width="200" Height="300" ItemsSource="{Binding Source={StaticResource MyStringData}}"/>

        </StackPanel>
    </Window>

    and:

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace DataBindingProof
    {
        public class MyStrings : List<String>
        {
            private List<String> myList = new List<String>();

            public MyStrings()
            {         
               
    myList.Add("Hello");
               
    myList.Add("Goodbye");
               
    myList.Add("Heya");
               
    myList.Add("Cya");
            }
            public
    List<String> DisplayInfo()
            {
                 return
    myList;
            }
        }
    }


    HTH
    Wednesday, May 10, 2006 10:54 AM
  • What do you mean by "it doesn't work"? I have tested your code and the listbox displays the four items. Did you see something else?
    Wednesday, May 10, 2006 1:48 PM
    Moderator
  • Thanks for your help Thomas, but it still doesn't work:

    The listbox is displayed, but the content of the listbox is the characters of "DataBindingProof.MyStrings": d,a,t,a...n,g,s

    Any other idea ?

    Wednesday, May 10, 2006 1:56 PM
  • Indeed, in the code I've provided, maybe a converter is needed.

    But, I'm also test your code and this one run fine on my computer...

    Why did you say that it doesn't work ?
    Wednesday, May 10, 2006 5:31 PM
  • I use this in XAML window

    <Window x:Class="DataBindingProof.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:DataBindingProof"
        Title="DataBindingProof" Height="700" Width="700"
        >
      <StackPanel Orientation="Vertical">
        <StackPanel.Resources>
          <ObjectDataProvider ObjectType="{x:Type m:MyStrings}" x:Key="MyStringData"/>
        </StackPanel.Resources>
        <ListBox Name="theListBox" Width="200" Height="300" ItemsSource="{Binding Source={StaticResource MyStringData}}" />
        <Button Name="AddItem" Click="AddNewItem" />
      </StackPanel>
    </Window>

    I added this new Button and AddNewItem Code is as below

    public void AddNewItem(object sender, EventArgs args)
            {
                MyStrings.ms[0] = "hai";
                MyStrings.ms.Add("Hai");
            }

    I have Changed MyStrings as

        public class MyStrings : List<String>
        {
            public static MyStrings ms = null;
            public MyStrings()
            {
                this.Add("Hello");
                this.Add("Goodbye");
                this.Add("Heya");
                this.Add("Cya");
                ms = this;
            }
        }


    I see that on clicking the Button the listbox does not update automatically even though the object to which it is bound has changed. Does this code requires something else to do that

    Tuesday, May 30, 2006 7:03 AM
  • Inherit from ObservableCollection<string> instead of List<string> to have the notifications fired when the new item is added.
    Tuesday, May 30, 2006 7:07 PM