简体   繁体   中英

TextBox UpdateSourceTrigger for PropertyChanged and LostFocus

Hello I have question about a wpf/xaml text box c# implementation.

I am trying to figure out how in my c# code to know what the UpdateSourceTrigger is being used. I am a newbie, so I would very much appreciate if people are patient with me and helpful.

In my C# I need to know how the data in the Text box is trying to be accessed using UpdateSourceTrigger. I know the property changed when my OnPropertyChanged() is called. But I also need to know how if the user is trying to use LostFocus or PropertyChanged in the C# code. This is so I can do some special processing for either case.

xaml

<TextBox>
    <TextBox.Text>
        <Binding Source="{StaticResource myDataSource}" Path="Name"
        UpdateSourceTrigger="PropertyChanged"/>
    </TextBox.Text>
</TextBox>    

c#

protected void OnPropertyChanged(string name)
{
    // If UpdateSourceTrigger= PropetyChanged then process one way
    // If UpdateSourceTrigger= LostFocus then process one way
}

Is there any other methods that get called when using LostFocus?

Thanks you

You will have to get a reference to your TextBlock and get the binding expression then you will have access to the Binding information

Example:(no error/null checking)

<TextBox x:Name="myTextblock">
     <TextBox.Text>
        <Binding Source="{StaticResource myDataSource}" Path="Name"
        UpdateSourceTrigger="PropertyChanged"/>
     </TextBox.Text>
</TextBox>


var textblock = this.FindName("myTextBlock") as TextBlock;
var trigger = textblock.GetBindingExpression(TextBlock.TextProperty).ParentBinding.UpdateSourceTrigger;
// returns "PropertyChanged"

another way of getting the binding object is:

Binding binding = BindingOperations.GetBinding(myTextBox, TextBox.TextProperty);

if (binding.UpdateSourceTrigger.ToString().Equals("LostFocus"))
{

}
else
{

}

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