ItemsControl(DataTemplate)内でBindingを行うとItem要素のBindingとなり、
ItemsControlの外とバインディングのターゲットが異なります。
そうなるともう、ItemsControl内では親(Window)のバインディングのターゲットは取得できないと思っていました。
でも、よく考えたら、XAMLではWindowのバインディングのターゲットはDataContextプロパティに格納します。
ということは、Windowの要素を取得できればいいわけです。
そこで「RelativeSource」を使います。
詳しい説明は過去の記事を参照ください。
final.hateblo.jp
例としてItemsControl内からWindowのDataContextに対してBindingするコードです。
Movselexでも以下と似たようなコードを使用してたりします。
<Window x:Class="LivetWPFApplication1.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" Title="MainWindow" Height="350" Width="525"> <Grid> <ItemsControl ItemsSource="{Binding InputParams}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Value.Title}" Width="{Binding Path=DataContext.TitleWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"></TextBlock> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Grid> </Window>
ItemControl内のTextBlockでTextはItemsSourceから取得していますが、Widthは親のバインディングターゲット(WindowのDataContext)から取得しています。
ちなみにWindowでなくても、UserControlでも同様に使用できます。
これは典型的なRelativeSourceの使用例ではないかと思います。