2012-01-26 9 views
0

私はWPFが新しく、ObjectDataProviderを使用してオブジェクトを作成し、コードの背後で使用しようとしています。具体的には、テキストがテキストボックスに入力されたときにオブジェクトを更新したいと思います。ここでは、XAMLは次のとおりです。ここでObjectDataProviderを使用してオブジェクトをインスタンス化する

<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> 

は、コードが背後にある:

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"); 
    } 
} 

私が操作できるように、私の質問は、私がたObjectDataProviderによって作成されたオブジェクトを参照するために何を使うのですかれます作成したMyStringのインスタンスありがとう。

新しい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> 

新しいコードの後ろに:

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"); 
    } 
} 

答えて

3

はあなたがObjectInstance財産で見たことがありますか?

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

あなたは次のように、あなたのObjectDataProviderに名前を付ける場合:あなたが行うことができます背後にあるコードの中で次に

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

myProvider.ObjectInstance 

NULLをチェックすることを忘れないでください。実際に作成されていることを確認してください。

+0

私は「この名前でmyProviderを重複して登録することはできません」と言っているのですが、なぜですか? – BobD

+0

@BobDあなたの質問にあるコードを更新して、 XAMLで名前を付けた後、コードビハインドで宣言したように聞こえます。 – CodingGorilla

+0

@コーディングゴリラ - 編集を参照してください - ありがとう。 – BobD

関連する問題