BobD
BobD

Reputation: 11

Using ObjectDataProvider to instantiate an object

I am new to WPF and I am trying to us ObjectDataProvider to create an object then use it in the code behind. More specifically I would like to update the object when text is entered into a textbox. Here is the xaml:

<Window x:Class="bindings.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local ="clr-namespace:bindings"

    Title="Bindings" Height="410" Width="1044">
<Window.Resources>
    <ObjectDataProvider x:Key ="MyStringData" ObjectType="{x:Type local:MyStrings}" />
</Window.Resources>

<StackPanel>
    <TextBox Height="23" Name="textBox1" Width="120" KeyDown="textBox1_KeyDown" />
    <ListBox Name="theListBox" Width="200" Height="79" 
  ItemsSource ="{Binding Source={StaticResource MyStringData}}"/>
</StackPanel>

Here is the code behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Return)
        {

        }

    }
}
public class MyStrings : List<String>
{
    public MyStrings()
    {
        this.Add("Test1");
        this.Add("Test2");
        this.Add("Test3");
        this.Add("Test4");
    }
}

My question is what do I use to refer to the object created by the ObjectDataProvider so I can manipulate the instance of MyStrings that it created. Thanks.

New xaml:

<Window x:Class="bindings.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local ="clr-namespace:bindings"
    Title="Bindings" Height="410" Width="1044">
<Window.Resources>
    <ObjectDataProvider x:Key ="MyStringData" ObjectType="{x:Type local:MyStrings}" x:Name="myProvider" />
</Window.Resources>
<StackPanel>
    <TextBox Height="23" Name="textBox1" Width="120" KeyDown="textBox1_KeyDown" />
    <ListBox Name="theListBox" Width="200" Height="79" 
  ItemsSource ="{Binding Source={StaticResource MyStringData}}"/>
</StackPanel>

New Code Behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Return)
        {
            MyStrings temp = myProvider.ObjectInstance; 
        }

    }
}
public class MyStrings : List<String>
{
    public MyStrings()
    {
        this.Add("Test1");
        this.Add("Test2");
        this.Add("Test3");
        this.Add("Test4");
    }
}

Upvotes: 0

Views: 3493

Answers (1)

CodingGorilla
CodingGorilla

Reputation: 19842

Have you looked at the ObjectInstance property?

http://msdn.microsoft.com/en-us/library/system.windows.data.objectdataprovider.objectinstance.aspx

If you give your ObjectDataProvider a name, like:

<ObjectDataProvider x:Key ="MyStringData" ObjectType="{x:Type local:MyStrings}" x:Name="myProvider" />

Then in your code behind you can do:

myProvider.ObjectInstance

Don't forget to check for NULL to make sure it's actually been created.

Upvotes: 3

Related Questions