Weiβlogy

Logo

作ったもの、気づいたこと、試したこと。元エンジニアの視点で技術も日常もつづる雑多ブログ「Weiβlogy」。

View the Project on GitHub weizlogy/blog

2010/09/11

cannot find source for binding with reference について

掲題の件について、検索キーワードにあがりましたので、簡単にまとめました。 WPFアプリケーション開発中に、ListBoxItemなどのStyle定義で RelativeSource FindAncestor を使用したBindingを設定すると、特定の条件下で「Cannot find source for binding with reference」というエラーに遭遇することがあります。この記事では、そのエラーメッセージと、考えられる原因について、具体的なXAMLコードを交えながら解説します。

正式には、以下のErrorが発生しました。

System.Windows.Data Error: 4 : Cannot find source for binding with reference ‘RelativeSource FindAncestor, AncestorType=’System.Windows.Controls.ItemsControl’, AncestorLevel=’1’’. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is ‘ListBoxItem’ (Name=’’); target property is ‘HorizontalContentAlignment’ (type ‘HorizontalAlignment’)

System.Windows.Data Error: 4 : Cannot find source for binding with reference ‘RelativeSource FindAncestor, AncestorType=’System.Windows.Controls.ItemsControl’, AncestorLevel=’1’’. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is ‘ListBoxItem’ (Name=’’); target property is ‘VerticalContentAlignment’ (type ‘VerticalAlignment’)

xamlのStyle定義のSetterプロパティでAlignmentを定義していると出るみたいです。 定義を外すとErrorは出ません。 みたいです。というのは、何故なのか理由が分からないためです。

以下、実際のコードの抜粋です。

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>