John Goodman
John Goodman

Reputation: 59

WPF TextChanged event fired only when I change the value

In my project I have several textboxes with a method binded to TextChanged event:

<TextBox Grid.Column="12" Style="{StaticResource txtDataStyle1}" Width="100" TextChanged="Data_TextChanged">
    <Binding Path="ConfigObject.Edit.Default" UpdateSourceTrigger="LostFocus">
        <Binding.ValidationRules>
            <local:GenericValidationRule>
                <local:GenericValidationRule.Wrapper>
                    <local:Wrapper TipoInterno="{Binding Path=Content, Source={x:Reference txtTipo}}"/>
                </local:GenericValidationRule.Wrapper>
            </local:GenericValidationRule>
        </Binding.ValidationRules>
    </Binding>
</TextBox>

When the page loads and the textboxes are valorized with "ConfigObject.Edit.Default", the Data_TextChanged event is fired. How can omit it? I would "use" that method only when I change its value. Any help?

Upvotes: 0

Views: 293

Answers (1)

mm8
mm8

Reputation: 169400

Instead of handling the the TextChanged event in the view, you should implement your logic in the setter of the source property.

If you can't do this for some reason, you could simply return from the event handler if the TextBox hasn't yet been loaded:

private void Data_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox textBox = (TextBox)sender;
    if (textBox.IsLoaded)
    {
        //your code...
    }
}

Upvotes: 2

Related Questions