Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 way binding in wpf calls property twice

Ive been playing with wpf and 2 way data binding to better understand it and ive noticed that when a textbox has 2 way data binding to a property the property is called twice. I have verified this by writing a value to the output window when the property is called. My code is below:-

My xaml

<Page
    x:Class="_2waybindTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:_2waybindTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <TextBox HorizontalAlignment="Left" Margin="55,93,0,0" TextWrapping="Wrap" Text="{Binding TestProperty, Mode=TwoWay}" VerticalAlignment="Top" Width="540"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="55,31,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
        <TextBox HorizontalAlignment="Left" Margin="55,154,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="540"/>
    </Grid>
</Page>

my simple viewmodel class to test

public class viewmodel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _TestProperty;

    public void SetTestProperty()
    {
        this.TestProperty = "Set Test Property";
    }

    public string TestProperty{
        get
        {
            return this._TestProperty;
        }
        set
        {
            this._TestProperty = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("TestProperty"));
            }

            Debug.WriteLine("this._TestProperty = " + this._TestProperty);
        }
    }
}

my xaml code behind

/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        DataContext = new viewmodel();
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var vm = (viewmodel)DataContext;
        vm.SetTestProperty();
    }
}

Why is it called twice. Is this expected behaviour?

like image 562
Richard Banks Avatar asked Apr 24 '13 14:04

Richard Banks


1 Answers

Generally, you should check if value actually changed, before firing a propertyChanged event, otherwise you may get into infinte cycle of binding updates. In your case, textbox is probably checking for change, preventing such cycle.

public string TestProperty{
    set
    {
        if(this._TestProperty == value)
        {
            return;
        }

        this._TestProperty = value;

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("TestProperty"));
        }            
    }
}
like image 200
alex Avatar answered Sep 20 '22 03:09

alex