I am not sure if my question header represent exactly my problem, I will do the best to explain:
I have a grid cell DataTemplate: (the grid belong to third party company but it`s not important for my question)
<DataTemplate>
<TextBlock>
<Hyperlink Command="{Binding OpenLinkCommand}">
<Hyperlink.ToolTip>
<TextBlock Text="{Binding Data.MapLink}"/>
</Hyperlink.ToolTip>
<TextBlock Text="{Binding Data.MapLink}" TextDecorations="underline">
</Hyperlink>
</TextBlock>
</DataTemplate>
I want make this DataTemplate to show some hyperlink ("Data.MapLink" is the object which contain the link value) and each click on this link will fire the command "OpenLinkCommand".
The problem is that "Data.MapLink" and "OpenLinkCommand" are located in different dataContext and then I have to choose one of the next choices:
leave hyperlink dataContext as it - the command won`t work and the hyperlink will get the Data.MapLink value.
change hyperlink dataContext to the command datacontext - The command will work but the hyperlink name will be empty.
Regretfully I don`t have option put those items in same dataContext so I must find a way how to tell the command that it dataContext is "X" and tell the hyperLink that it dataContext is "Y".
I am hoping that my question is clear How can I solve this problem?
There are some binding properties you can use to specify a different Source
for your binding than the default DataContext
The most common ones are ElementName
or RelativeSource
, which will find another UI element in the VisualTree so you can bind to it's properties.
For example, the following uses ElementName
to tell the binding that it should use MyGridView
as the binding source, and to bind to MyGridView.DataContext.OpenLinkCommand
<Hyperlink Command="{Binding ElementName=MyGridView,
Path=DataContext.OpenLinkCommand}">
You can also use RelativeSource
in a binding to find an object further up the VisualTree of the specified object type, and use it as the binding source. This example does the same thing as the above example, except it uses RelativeSource
instead of ElementName
, so your GridView
doesn't need to have a Name
specified.
<Hyperlink Command="{Binding
RelativeSource={RelativeSource AncestorType={x:Type GridView}},
Path=DataContext.OpenLinkCommand}">
A third option is to set the binding's Source
property to a static object, like this:
<Hyperlink Command="{Binding
Source={x:Static local:MyStaticClass.OpenLinkCommand}}">
Based on your comment here about binding to a singleton, this would probably be the best option for you.