2011-11-18 4 views
2

여러 항목이 포함 된 컨테이너 유형 컨트롤이 있습니다. 컨테이너 컨트롤에는 이 있으며이 항목에는 ItemsControlDataTemplate이 포함되어 있습니다. 그러나 항목은 컨테이너 컨트롤에서 무언가에 바인딩해야합니다. 다음은 간단한 예입니다.ItemsControl.ItemTemplate 내에서 부모 DataTemplate에 바인딩하는 방법

<DataTemplate DataType="{x:Type ContainerType}"> 

    <!-- Display of the container stuff--> 

    <ItemsControl ItemsSource="{Binding Items, Mode=OneWay}"> 

     <ItemsControl.ItemTemplate> 
       <DataTemplate DataType="{x:Type Item}"> 

        <!-- Display of the item stuff --> 
        <ComboBox Text="Choose a container-level option..." 
          ItemsSource="{WHAT GOES HERE?}"/> 

       </DataTemplate> 
     </ItemsControl.ItemTemplate> 

     </ItemsControl> 
</DataTemplate> 

컨테이너 수준까지 항목 수준에서 무언가를 묶는 방법은 무엇입니까?

답변

1

아마도 TemplateBinding을 사용 하시겠습니까? 같은

뭔가 :

{TemplateBinding YourPropertyInTheDataTemplateContext} 
+1

TemplateBinding에만 작동 ControlTemplates에 대한 : 기본적으로 당신은 당신이 같은 외부 레벨 컨트롤의 이름을 확인하십시오 x:Name="MainWin"을하고 당신은 이런 식으로 뭔가를 할 수 있습니다. –

6

당신이 사용할 수있는 RelativeSource

<ComboBox ItemsSource="{Binding SomeCollection, 
       RelativeSource={RelativeSource 
        AncestorType={x:Type local:MyContainerControl}}}"/> 

당신이 당신의 바인딩 경로를 사용하는 컬렉션의 위치에 따라 바인딩. MyContainerControlDependencyProperty으로 위치하면 위 바인딩이 정상적으로 작동합니다. DataContextMyContainerControl 인 경우 바인딩 경로를 DataContext.SomeCollection으로 설정해야합니다.

+0

컬렉션은 ContainerType DataTemplate의 DataContext에있는 속성이므로 {ItemsSource = "{Binding RelativeSource = {RelativeSource FindAncestor, AncestorType = {x : 유형 로컬 : 컨테이너 유형}}, Path = DataContext.AvailableJobs}'도 시도했습니다. 위의 양식은 작동하지 않았습니다 ... –

+0

@BrianTriplett 바인딩에서 FindAncestor를 제거하십시오. – Rachel

+0

미안하지만 ComboBox에 콜렉션을 표시하지 않습니다. 'MyContainerControl'을 말하면'ContainerType '예제 코드에서 또는'ContainerType' (이 경우 'ContentControl')에 대한 DataTemplate을 포함하는 컨트롤을 참조하고 있습니까? –

1

저는 항상 ElementName의 큰 팬이었습니다. 내가 아는

<DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
       <ComboBox ItemsSource="{Binding ElementName=MainWin, Path=DataContext.SomeCollection}"/> 
+0

이것은 작동하지 않습니다. DataTemplate은 시각적 트리에서 'ElementName'을 통해 참조 될 수 있도록 명확한 이름을 얻지 못합니다. –

+0

나는 그렇게 생각하곤했지만 WPF 프로젝트에서 작업하고 있음을 확신합니다. 그냥 테스트 해 봤어. Silverlight를 사용하고 있습니까? 여기 내 실제 작업 코드에서 컷/과거의 : –

+0

' <단추 명령 = "{바인딩 DeleteSkillSetCommand , ElementName = MainWin} "CommandParameter ="{Binding ''} "스타일 ="{StaticResource SimpleButtonStyle} "여백 ="5 "x : 이름 ="DeleteSkillSet "VerticalAlignment ="Center "HorizontalAlignment ="Left "> <이미지 소스 = "images/RedX.png"Height = "16"Width = "16"/> –

관련 문제