2010-06-05 8 views
12

내 출력 창에이납니다 :WPF : 바인딩 오류를 어떻게 디버깅합니까?

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')

이것은 실행이 나는 또한 Bea Stollnitz's article를 추천하려고했지만 조나단 알렌은 자신의 게시물을 가지고

 <GroupBox Header="Grant/Deny Report"> 
      <ListBox ItemsSource="{Binding Converter={StaticResource MethodBinder}, ConverterParameter=GrantDeny, Mode=OneWay}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <Label Content="{Binding Entity}"/> 
          <Label Content="{Binding HasPermission}"/> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </GroupBox> 

답변

8

올바른 보이는 내 XAML입니다 나는 아직도 이것을 타이핑했다. 나는 또한 this blog entry에있는 연결을 추천한다.

이 특별한 경우에 ListBoxItem에 ItemsAccess에 대한 FindAncestor 바인딩이있는 것을 볼 수 있습니다. 즉, 거기에 바로 알려줍니다 어딘가에 ListBoxItem의입니다 중 하나입니다

  1. 하지 않음 (목록 상자는 ItemsControl에있다)

또한하지 ItemsControl에 아래의 시각적 트리, 또는

  • 에서, 어딘가 누군가가 ListBoxItem의 VerticalContentAlignment 속성을 FindAncestor에 바인딩한다는 것을 알고 있습니다. 시스템 테마를 살펴보면

    (NET 리플렉터의 BAMLViewer 추가 기능을 통해 Expression Blend를 또한 사용할 수와 함께 제공), 우리는이를 참조하십시오

    바인딩이 어디에서 오는지이 설명
    <Style x:Key="{x:Type ListBoxItem}"> 
        <Setter Property="VerticalContentAlignment" 
          Value="{Binding Path=VerticalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}" /> 
    

    . 다음 질문은 ListBox (또는 다른 ItemsControl) 아래에없는 ListBoxItem이 어떻게 생성되는 것입니까? 찾기 위해

    어떤 것들은 :

    • 당신은 어디 코드에서 ListBoxItems을 구성하고 있습니까?
    • XAML에 명시 적으로 지정된 ListBoxItem이 있습니까?
    • ListBox의 항목을 수동으로 조작하는 코드가 있습니까?

    바라기를 이것은 올바른 방향으로 향할 수 있기를 바랍니다.

  • +0

    아니요, 아니요, 아니요. 흥미로운 점은 ItemsSource가 데이터 테이블이라는 것입니다. –

    +0

    하지만 적어도 스타일 시트에 있음을 알기에 충분한 정보를 주셨습니다. –

    3

    내 데이터 바인딩 오류가 정보로 표시되었지만 TreeView에서 비슷한 문제가 발생했습니다.

    TreeViewItem의 TreeView 리소스에 암시 적 스타일을 정의하여이 문제를 해결했습니다. 그 스타일 내에서 필자는 수직 및 수평 콘텐츠 정렬 속성이 없음을 정의했습니다.

    <TreeView.Resources> 
        <Style TargetType="{x:Type TreeViewItem}" > 
          <Setter Property="VerticalContentAlignment" Value="Stretch"/> 
          <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
        </Style> 
    </TreeView.Resources> 
    
    +0

    나는이 같은 문제에 직면하여 귀하의 접근 방식을 사용하여 해결했습니다. 감사! – cordialgerm

    관련 문제