WPF:数据与code绑定 [英] WPF: Data binding with code
本文介绍了WPF:数据与code绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何使用数据绑定从code(C#或VB)?
这是我到目前为止,但它显示 Binding.ToString
而不是 m_Rep.FirstName
。
公共只读属性TabCaption作为对象
得到
返回新标签用{.Foreground = Brushes.Black,.Content =新绑定(名字),.DataContext = m_Rep}
到底得的
高端物业
解决方案
是的,在code结合是直的分配(这是XAML如何使它看起来像它的工作原理)有一点不同。
我可以给你在C#中的例子 - 不应该太远VB.NET中删除
VAR标签=新标签{前景= Brushes.Black,DataContext的= m_Rep};
label.SetBinding(Label.ContentProperty,新的绑定(名字));
返回标签;
所以SetBinding的方法绑定(在DataContext的)中的名字路径标签的内容属性。
How do I use data-binding from code (C# or VB)?
This is what I have so far, but it is displaying Binding.ToString
instead of m_Rep.FirstName
.
Public ReadOnly Property TabCaption As Object
Get
Return New Label With {.Foreground = Brushes.Black, .Content = New Binding("FirstName"), .DataContext = m_Rep}
End Get
End Property
解决方案
Yes, binding in code is a little different from straight assignment (which is how XAML makes it look like it works).
I can give you an example in C# - shouldn't be too far removed from VB.NET.
var label = new Label { Foreground = Brushes.Black, DataContext = m_Rep };
label.SetBinding(Label.ContentProperty, new Binding("FirstName"));
return label;
So the "SetBinding" method binds the "FirstName" path (of the DataContext) to the label's Content property.
这篇关于WPF:数据与code绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文