[英]Databinding a WPF Textbox in Winforms
I am using a WPF textbox inside an element host in a Winforms project. 我在Winforms项目的元素宿主中使用WPF文本框。
I would like to databind this textbox to a bindingsource as I do with the standard winforms textbox like so: 我想像使用标准winforms文本框一样,将此文本框数据绑定到绑定源:
Me.tbxCorrectiveAction.DataBindings.Add("Text", bgsTasks, "CorrectiveAction", False)
This is what I have tried: 这是我尝试过的:
Dim host As New System.Windows.Forms.Integration.ElementHost()
Dim wpfTextBox As New System.Windows.Controls.TextBox()
wpfTextBox.SpellCheck.IsEnabled = True
host.Dock = DockStyle.Fill
host.Child = wpfTextBox
Me.Panel8.Controls.Add(host)
Dim binCorrectiveAction As New System.Windows.Data.Binding("CorrectiveAction")
binCorrectiveAction.Source = bgsTasks
wpfTextBox.SetBinding(System.Windows.Controls.TextBlock.TextProperty, binCorrectiveAction)
Solutions in either VB or C# are fine. VB或C#中的解决方案都可以。
Try this: 尝试这个:
UPDATE . 更新 。
There's an error in your code (or just a typo, which causes a logical error). 您的代码中有一个错误(或者只是一个错字,这会导致逻辑错误)。
You're trying to bind TextBlock .TextProperty on a TextBox control. 您正在尝试在TextBox控件上绑定TextBlock .TextProperty。
There should be TextBox.TextProperty
: 应该有TextBox.TextProperty
:
var dataTable = new DataTable();
dataTable.Columns.Add("Id", typeof(Int32));
dataTable.Columns.Add("Name", typeof(String));
dataTable.Rows.Add(1, "John");
dataTable.Rows.Add(2, "Mary");
dataTable.Rows.Add(3, "Peter");
dataTable.Rows.Add(4, "Helen");
var bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
var binding = new System.Windows.Data.Binding("Name");
binding.Source = bindingSource;
binding.UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged;
var textBox = new System.Windows.Controls.TextBox();
textBox.SpellCheck.IsEnabled = true;
textBox.SetBinding(System.Windows.Controls.TextBox.TextProperty, binding);
elementHost1.Child = textBox;
The reason is that these dependency properties (and controls) are differ , in spite of they have similar names. 原因是尽管这些依赖项属性(和控件)具有相似的名称,但它们是不同的 。
If it isn't a typo, then I recommend you to read about WPF dependency properties mechanism here . 如果不是拼写错误,那么我建议您在此处阅读有关WPF依赖属性机制的信息 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.