简体   繁体   中英

WPF TextBox_LostFocus event to binding object

So i have this TextBox template inside my ListView :

<DataTemplate x:Key="textboxCell">
              LostFocus="TextBox_LostFocus"/>
</DataTemplate>

And my event :

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
   // Get cell value by using sender Object
    string str= ((TextBox)sender).Text;
}

So as you can see i am get my TextBox value but how can i get my Binding object ?

So as you can see i am get my TextBox value but how can i get my Binding object ?

Cast the DataContext of the TextBox to your data object:

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    var textBox = sender as TextBox;
    if (textBox != null)
    {
        YourClass dataContext = textBox.DataContext as YourClass;
    }
}

Or, if you want to get a reference to the actual binding, you could use the BindingOperations.GetBinding method:

var binding = BindingOperations.GetBinding(textBox, TextBox.TextProperty);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM