setting a Window DataContext to a property object of App

Pablo gil 86 Reputation points

I want to set in xaml, the DataContext of a window to a property object of App,
but I do not know how to configure the binding, I do not know the RelativeSource
of App to set in MainWindow.xaml

DataContext="{Binding RelativeSource=???? , Path=myAppProperty }"

and then use it, for example like this:

        <TextBlock Text="{Binding Path=myField}"/>

maybe, I should use another property of the binding instead of RelativeSource

could someone help?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
789 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 114.5K Reputation points

    Try two approaches:

    <TextBlock Text="{Binding myAppProperty}" DataContext="{x:Static Application.Current}" . . .  />
    <TextBlock Text="{Binding myAppProperty, Source={x:Static Application.Current}}"  . . . />

    This assumes that App contains the corresponding property. A simple one is:

    public string myAppProperty { get; set; } = "SomeText";