Osman GUMUS
Osman GUMUS

Reputation: 61

Template10 PageHeader Text Binding Exception

I'm using Template10 for my UWP application and I want to update page header text dynamicly. Here is my XAML,

        <controls:PageHeader x:Name="pageHeader" Text="{Binding ViewModel.Title}">
            <!--place stretched, across top-->
            <RelativePanel.AlignTopWithPanel>True</RelativePanel.AlignTopWithPanel>
            <RelativePanel.AlignRightWithPanel>True</RelativePanel.AlignRightWithPanel>
            <RelativePanel.AlignLeftWithPanel>True</RelativePanel.AlignLeftWithPanel>
        </controls:PageHeader>

Here is the exception message,

WinRT information: Failed to assign to property 'Template10.Controls.PageHeader.Text'. [Line: 57 Position: 73]

Additional information: The text associated with this error code could not be found.

Failed to assign to property 'Template10.Controls.PageHeader.Text'. [Line: 57 Position: 73]

Without binding it works perfectly, but i need to dynamicly change it, any idea/workaround? Thanks.

Upvotes: 0

Views: 400

Answers (2)

user5525674
user5525674

Reputation: 921

If you haven't solved this problem already (has been a while) here is what you should do.

First, make sure that the ViewModel is defined either in XAML page or code-behind page.

<Page.DataContext>
    <vm:ViewModelPage x:Name="ViewModel" />
</Page.DataContext>

vm is is the namespace for your ViewModel (e.g., MyAppProject.ViewModels) defined like below in the namespace declarations in the top:

xmlns:vm="using:MyAppProject.ViewModels"

while ViewModelPage is the the Type of ViewModel page that contains the Title property.

The same can be defined in view page code-behind as below (but prefer XAML as its is more readable in conjunction with you bindings in there)

public ViewModelPage ViewModel => this.DataContext as ViewModelPage ;

You need to put the using namespace declaration of the ViewModel at the top in the code-behind (in effect what you did for vm in XAML page).

With the ViewModel definition done right, you shouldn't get the "...could not be found" error.

Now for the binding: Use x:Bind because it's an improvement over the classical binding. See my answer here explaining the advantages of x:Bind. One important thing to remember is setting the binding Mode to "OneWay" (or "TwoWay" if this is applicable in some cases like updating a TextBox) rather than leave it to the default "OneTime" if Mode definition is missing.

Last but not least, ensure that the Title property raises the PropertyChanged event to update the binding (if you change the title dynamically). The ViewModel page should be well geared up to facilitate this: If you follow the Template 10 pattern, the ViewModel page derives from ViewModelBase and properties have access to the RaisePropertyChanged method call, so your Title Property should look like:

private string _title;
public string Title
{
    get { return _title; }
    set { _title = value; RaisePropertyChanged(nameof(Title)); }
}

A different method name may be used for RaisePropertyChanged, such as Set or SetProperty. Sorry, if this comes a bit late but hope it helps.

Upvotes: 0

Osman GUMUS
Osman GUMUS

Reputation: 61

I've found the problem, instead of Binding I've used x:Bind, but there should not be difference except performance, still need to understand why..

Text="{x:Bind ViewModel.Title}">

Upvotes: 0

Related Questions