Use of ObjectDataProvider ?

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


Answer this question

Use of ObjectDataProvider ?

  • memi0

    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


  • Damir Bersinic

    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


  • Ruskin Dantra

    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


  • AYHAN

    Inherit from ObservableCollection<string> instead of List<string> to have the notifications fired when the new item is added.
  • boulderbum

    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


  • Brian R. Bondy

    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


  • Use of ObjectDataProvider ?