开发者

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 c开发者_StackOverflow中文版odebehind 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜