[英]WPF TextBox_LostFocus event to binding object
所以我在ListView
有这个TextBox
模板:
<DataTemplate x:Key="textboxCell">
LostFocus="TextBox_LostFocus"/>
</DataTemplate>
而我的event
:
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
// Get cell value by using sender Object
string str= ((TextBox)sender).Text;
}
如您所见,我可以获取TextBox
值,但是如何获取Binding object
呢?
如您所见,我可以获取
TextBox
值,但是如何获取Binding
对象呢?
将TextBox
的DataContext
强制转换为数据对象:
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
var textBox = sender as TextBox;
if (textBox != null)
{
YourClass dataContext = textBox.DataContext as YourClass;
}
}
或者,如果要获取对实际绑定的引用,则可以使用BindingOperations.GetBinding
方法:
var binding = BindingOperations.GetBinding(textBox, TextBox.TextProperty);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.