Bindingのときに指定するRelativeSourceってのがある。
これをちょびっと整理してみたよ。
とりあえず、よく使うものはSelfとTemplatedParentの2つくらいかなぁ。
変わり者として、AncestorTypeというものもある。
Selfは自分自身をBindingのソースに指定する。
TemplatedParentは、テンプレートの親…そのまんまだ。
AncestorTypeというのは、自分の親・親と辿っていき、AncestorTypeで指定してものをソースにする。
簡単な実験プログラム!!
<Window x:Class="WpfRelative.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBlock Text="{Binding RelativeSource={RelativeSource Self}, Path=FontFamily}" />
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}},Path=Orientation}" />
<Button Content="Hello world">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</Window>
最初のBindingではRelativeSourceにSelfを指定している。BindingのRelativeSourceプロパティにはRelativeSourceマークアップ拡張を使って設定するみたいだ。
ちょっと冗長な気もするけど仕方ない。
これは自分自身のフォントファミリーを表示する。Vistaではデフォルトだとメイリオになってる。
次のTextBlockでは、AncestorTypeを指定している。型に指定しているのはStackPanelなので、Windowの直下にあるStackPanelが対象になる。
そのStackPanelのOrientationプロパティの値が表示される。この場合は、Verticalになる。
3つ目のBindingは、ControlTemplateの中で使ってる。この場合は、TemplatedParentはButtonになる。
それの、Contentを指定してるのでHello worldと表示される。
TemplatedParentは、TemplateBindingと同じ動きをしてるのかなぁ。
要検証!!
後、軽く見ただけだとよくわからないものに、PreviousDataというものがある。
眠気に負けてこいつだけどうやって使うか調べてない…zzz