I am finding the need to bind to the DataContext
of a Page
which is set like this:
<d:Page.DataContext>
<designTime:PayeesPageDesignViewModel />
</d:Page.DataContext>
At design time. However, I need to bind to it from a child control that has a different data context:
<GridView x:Name="PayeesGridView"
Margin="0,30,0,0"
IsItemClickEnabled="True"
ItemsSource="{Binding Payees}"
SelectionChanged="PayeesGridView_OnSelectionChanged">
Since the ItemsSource
is set for the GridView
, the DataContext
of my GridViewItems
is set to the individual Payee
objects in the Payees
collection. My ViewModel
has a property that I need to reference from the View
in a converter to determine the visibility of an item.
I can set the visibility based on a property of the Payee
object like this:
<Border Width="250"
Height="250"
Background="Gray"
Opacity="0.85"
Visibility="{Binding Path=IsOpen,
Converter={StaticResource AccountStatusToVisibilityConverter}}">
But what I really need to bind to is the Settings.ShowInactive
property of the Page
's DataContext
. Is there a way to grab that context from a child control? I am using WinRT, so I don't have the benefit of the FindAncestor
binding source.
EDIT
As suggested in the comments, I tried to change the binding to look like this:
Visibility="{Binding Path=DataContext.Settings.ShowInactivePayees, ElementName=PageName,
Converter={StaticResource AccountStatusToVisibilityConverter}}">
But when I set a breakpoint in the AccountStatusToVisibilityConverter
, the converter is never reached.
x:Name
and use anElementName
Binding? – ClemensElementName="PageName.DataContext"
andPath="Settings.ShowInactivePayees"
but the being passed isnull
even though theDesignTimeViewModel
is hard-coded to befalse
and I try at runtime withShowInactivePayees
set totrue
orfalse
. – dub styleePageName.DataContext
is not an element name. Your binding should certainly look like{Binding Path=DataContext.Settings.ShowInactive, ElementName=PageName}
. – ClemensElementName="PageName.DataContext" Path="Settings.ShowInactivePayees"
andElementName="PageName" Path="DataContext.Settings.ShowInactivePayees"
were the same? Anyway, I am still gettingnull
as the value being passed, regardless of the value ofShowInactivePayees
. – dub styleex:Name="PageName"
and the compiler is recognizing everything, but I don't see why the converter is no longer firing. – dub stylee