Reputation: 35400
Is there a easy hack for doing the following?
<Binding RelativeSource="{RelativeSource AncestorType=UserControl OR Window}" Path="Tag" />
I simply want to bind to the top-level parent's Tag property, which could either be UserControl or Window. Note however that the distance from the current control to the parent is arbitrary, so I can't use AncestorLevel
.
Upvotes: 1
Views: 537
Reputation: 313
You can just use MultiBinding
instead of Binding
for this, and then make a simple IMultiValueConverter
that finds the first item in "values" that isn't null (or DependencyProperty.UnsetValue
, which tends to pop up when working with the Visual Tree.
It's a less hacky way to do this, and works perfectly for me.
Upvotes: 0
Reputation: 42991
Well if it's a hack you want :)
public partial class MainWindow : ITopLevel
{
public MainWindow()
{
InitializeComponent();
Tag = "I'm at the top";
}
}
public interface ITopLevel
{
// optionally specify Tag in the interface, it will work either way
object Tag { get; set; }
}
<Grid>
<Button Content="{Binding Path=Tag, RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type Demo:ITopLevel}}}"/>
</Grid>
Upvotes: 3