0
votes

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

2

2 Answers

2
votes

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"
1
votes

another way of getting the binding object is:

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

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

}
else
{

}