2017-06-01 6 views
0

FrameworkElementFactoryはコード内に作成されたテキストボックスのように機能するので、xamlコードはなく、コード内にデータバインディングでこのテキストボックスを設定します。今私はこのテキストボックスのデータバインドを更新したいキーを押してください。私はこのLinkの添付された動作のための1つの方法を読んだが、xamlコードで動作するようだ。コードの背後にアタッチされた動作を設定する方法はありますか?WPFの背後にあるコードにEnterキーでデータバインドを更新する

ListBox DDF_List = new ListBox(); 

FrameworkElementFactory Editable_TextBox = new FrameworkElementFactory(typeof(TextBox)); 
Binding text_binding = new Binding("Value"); 
Editable_TextBox.SetBinding(TextBox.TextProperty, text_binding); 

DataTemplate Text_Layout = new DataTemplate(); 
Text_Layout.VisualTree = Editable_TextBox; 
DDF_List.ItemTemplate = Text_Layout; 

答えて

0

あなたはほぼ確実にXAMLでDataTemplateでこれをやるべき。あなたが持っているのは、XAMLのすべてをやるための事例です。しかし、あなたがそれを難し​​いやり方にすることに尽力しているなら、ここでどうしたらいいのですか。ここitemNameTextBoxがテキストボックスでのC#でTextBoxにその添付プロパティを適用するには、今だ、

Editable_TextBox.SetValue(
    InputBindingsManager.UpdatePropertySourceWhenEnterPressedProperty, 
    TextBox.TextProperty); 

より一般的には:あなたのFrameworkElementFactory

、あなたはFrameworkElementFactory上の他の依存関係プロパティのようにそれを設定することができます。添付プロパティと

InputBindingsManager.SetUpdatePropertySourceWhenEnterPressed(itemNameTextBox, 
    TextBox.TextProperty); 

、このC番号:

var itemNameTextBox = new TextBox { Name = "itemNameTextBox" }; 

var binding = new Binding("ItemName") 
{ 
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
}; 

itemNameTextBox.SetBinding(TextBox.TextProperty, binding); 

// This is the line you're asking for: 
InputBindingsManager.SetUpdatePropertySourceWhenEnterPressed(itemNameTextBox, 
    TextBox.TextProperty); 

このXAMLの完全に同等です:

<TextBox 
    Name="itemNameTextBox" 
    Text="{Binding Path=ItemName, UpdateSourceTrigger=PropertyChanged}" 
    b:InputBindingsManager.UpdatePropertySourceWhenEnterPressed="TextBox.Text" 
    /> 
+0

おかげで、それは素晴らしい作品。しかし、Frameworkelementfactoryを使用して、このコンテキストの「itemNameTextBox」を置き換えることはできますか? –

+0

@SamXia絶対に。 'InputBindingsManager.SetUpdatePropertySourceWhenEnterPressed()'への呼び出しは、あなたが必要とする私のコードの唯一の部分です。テキストボックスを渡しただけですが、作成しました。私はそれが「XAMLと完全に同等」であるという私の主張を正当化するために、すべてのことをやっただけです。ポイントを明確にするために更新します。 –

+0

いいえ、私はFrameworkElementFactory型を入力にTextBox型に置き換えることを意味しますが、FrameworkElementFactoryがDependencyObjectではないことを教えてくれます。 –

関連する問題