Shadow
Shadow

Reputation: 2478

UWP Bindings not initialized after InitializeComponent

DataBindings are not loaded in UWP after calling InitializeComponent. Because of this I am getting errors in my application trying manipulate things, which usually are bound to, but on Navigation bindings are not loaded. What is proper way of manipulating bound properties in OnNavigated events?

<Page
x:Class="StackApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:StackApp"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid>
    <ComboBox x:Name="cb" ItemsSource="{x:Bind Data}" />
</Grid></Page>
public sealed partial class MainPage : Page
{
    Data Data { get; set; } = Whatever;
    public MainPage()
    {
        this.InitializeComponent();

        //this.Bindings.Initialize();
        //it can solve problem by manual binding loading
    }
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    cb.SelectedIndex = 0; // Crash, because ComboBox has no items loaded yet
}

Upvotes: 0

Views: 457

Answers (2)

mm8
mm8

Reputation: 169370

The bindings have not yet been resolved in OnNavigatedTo. From the docs:

Unlike in prior XAML platforms, the OnNavigated method is called before the visual tree is loaded. This has the following implications:

  • You cannot access a valid Parent property value from an override of OnNavigated. If you need to access the Parent property, do so in a Loaded event handler.
  • You cannot use OnNavigatedTo for element manipulation or state change of controls on the destination page. Instead, attach a Loaded event handler at the root of the newly loaded page's content, and perform any element manipulations, state changes, event wiring and so on in the Loaded event handler.

So to wait until the ComboBox has been loaded, you should handle the Loaded event:

public sealed partial class MainPage : Page
{
    Data Data { get; set; } = Whatever;
    public MainPage()
    {
        this.InitializeComponent();
        this.Loaded += MainPage_Loaded;
    }

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        cb.SelectedIndex = 0;
    }
}

Upvotes: 1

Antoine Besnard
Antoine Besnard

Reputation: 45

Are you getting errors from Output ? Where is your data class located ? Is the Datacontext set up ?

For example, if your xaml look like this :

<Page
x:Class="StackApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:StackApp"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid>
    <Button Content="{Binding}" />
</Grid></Page>

Then in your code behind you should set the DataContext like this :

public sealed partial class MainPage : Page
{
    String Test { get; set; } = "John";
    public MainPage()
    {
        this.InitializeComponent();
        DataContext = Test;
    }
}

Upvotes: 0

Related Questions