StefK
StefK

Reputation: 674

WPF textbox binding disappears after navigating forward and back again

I have a WPF-application with a mainwindow and different pages. On one of the pages (OverzichtPage) I have a textBox bound to a DataController (Data). (which is a dependencyProperty on the codebehind of the page) (Might be worth mentioning: the DataController is a Singleton, so that patient is supposed to stay the same and can't disapear..)

public static DependencyProperty data = DependencyProperty.Register("Data", typeof(DataController), typeof(OverzichtPage));
    public DataController Data
    {
        get { return (DataController)GetValue(data); }
        set { SetValue(data, value); }
    }


<TextBox Name="naamPatientTxtBox" Text="{Binding Path=Data.Patient.naam, Mode=TwoWay}" DataContext="{Binding ElementName=OP}" />

At first sight, this binding seems to work. When I navigate to another page by clicking a button

<Button Content="Meer info/ Wijzigen" Click="MeerInfoWijzigenBtn_Click" />

private void MeerInfoWijzigenBtn_Click(object sender, RoutedEventArgs e)
    {
        Uri pageFunctionUri = new Uri("View/ZorgTrajectPage1.xaml", UriKind.Relative);
        NavigationService.Navigate(pageFunctionUri);
    }

and navigate back, the binding suddenly stops working. I found out after the navigating back, the naamPatientTxtBox.GetBindingExpression(TextBox.TextProperty).ParentBinding; is empty. Does anyone have a clue why this binding suddenly disapears after the navigating? I really don't understand how this is possible.

Upvotes: 6

Views: 1828

Answers (2)

Emond
Emond

Reputation: 50672

Have you tried setting the KeepAlive property of the page to true? You might be running into history/caching problems. State isn't autmatically kept.

Upvotes: 6

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104741

I would put a breakpointat the Loaded event of the parent container (current Window or Page), check the DataContext property (does it contain anything?) and try to reset it if needed.

Another idea would be, set the TextBox.DataContext to Data, then the text to Patient.naam, in that way, it will be easier for you to debug it, also it will allow efficient DataContext inheritance.

Upvotes: 0

Related Questions