次のことを行うための簡単なハックはありますか?
<Binding RelativeSource="{RelativeSource AncestorType=UserControl OR Window}" Path="Tag" />
最上位の親の Tag プロパティ (UserControl または Window のいずれか) にバインドしたいだけです。ただし、現在のコントロールから親までの距離は任意であるため、使用できないことに注意してくださいAncestorLevel
。
それがあなたが望むハックならまあ:)
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>