2013-02-16 4 views
0

ContentTemplate을 사용하여 스타일이 지정된 ContentControl의 창이 있습니다.WPF (XAML) ListBox 내에서 바인딩 ItemTemplate

ContentTemplate은 간단한 ListBoxGrid 안에 중첩되어 있습니다. Grid는 코드를 사용하여 DataContext 속성을 설정합니다 (CollectionViewSource에 바인딩 됨 - cvs1이라고 함). ListBox ItemsSource은 Grid에서 상속되며 ListBox 항목의 채우기가 정상적으로 작동합니다. 예 :

<Grid x:Name="Grid1"> 
    <ListBox x:Name="ListBox1" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"/> 
</Grid> 

ListBox의 스타일이 지정되고 기본 스타일은 ResourceDictionary에 저장됩니다.

ItemTemplate을 사용하여 ListBox의 스타일 값을 설정하지만 DataTrigger을 사용하여 다른 Setter를 동적으로 적용하는 것이 좋습니다. 내가 직면하고있는 도전 과제는 DataTrigger 내에서 별도의 CollectionViewSource (Cvs2라고 부르 자)에 Binding을 설정할 수없는 것입니다.

<Style TargetType="{x:Type ListBox}"> 
    <Style.Triggers> 
     <!-- This seems to be trying to bind to cvs1, the error is it can't find the property --> 
     <DataTrigger Binding="{Binding cvs2, Path=TemplateName}" Value="ABC"> 
      <Setter Property="ItemTemplate"> 
     </DataTrigger> 

     <!-- This just doesn't seem to work --> 
     <DataTrigger Binding="{Binding Source={StaticResource cvs2},Path=TemplateName}" Value="XYZ"> 
      <Setter Property="ItemTemplate"> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

ResourceDictionary에는 cvs1과 cvs2가 정의되어 있습니다.

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="DataSources.xaml" /> 
</ResourceDictionary.MergedDictionaries> 

문제는 내가 ItemTemplate가 목록 상자에서의 DataContext를 상속한다 직면 할 것 그리고 내가 바인딩을 설정하기 위해이 문제를 얻이 수없는 것 :로 참조 다음

<CollectionViewSource x:Key="cvs1" /> 
<CollectionViewSource x:Key="cvs2" /> 

그리고

다음 cvs2 데이터 소스에 전달합니다. 나는 이것이 꽤 일상적인 StaticResource 바인딩 작업이라고 생각했다. 그렇지 않은 것 같습니다.

작동
<Label Content="{Binding cvs2, Path=/TemplateName}"/> 

이 레이블은 TEMPLATENAME의 값으로 채워집니다

나는 데이터를 디버깅 (메인 창에서) 그리드 외부의 라벨에 다음 코드를 테스트했습니다.

그러나 ItemsTemplate에있는 DataTrigger에서 이것을 사용하면 바인딩이 설정되지 않습니다.

ItemTemplate에서 cvs2에 대한 바인딩을 어떻게 설정합니까?

답변

0

저는 실버 라이트 개발자이며 이러한 유형 목표를 달성하기 위해 바인딩에서 변환기 만 사용합니다.

하지만 Xaml을 좋아하고 xaml에서 cvs2 개체의 TemplateName 속성에 따라 ItemTemplate을 변경하고 싶습니다. 그렇습니까?

그렇다면 ItemTemplate 속성에 값을 주시겠습니까?

 <DataTrigger Binding="{Binding Source={StaticResource cvs2}, 
        Path=TemplateName}" Value="XYZ"> 
     <Setter Property="ItemTemplate"> 
      <Setter.Value> 
       <TextBlock Text={Binding}/> 
      <Setter.Value> 
     </Setter> 
    </DataTrigger> 
    // When cvs2.TemplateName=XYZ use this template, isn't it? 

이 리소스보다 먼저 cvs2가 리소스로로드됩니까?

출력에 바인딩 오류가 있습니까?

x : Key가 암시 적이 아닌 spesific 스타일을 시도해 볼 수 있습니다.

값 변환기가 도움이 될 수 있습니다.

StaticResource를 사용하는 경우 계층 적으로 키의 맨 아래부터 보입니다. 그래서 ListBox의 datacontext에서 검색하지 않을 것입니다.

나는 아직 대답하지 못했지만 어쩌면 당신에게 아이디어를 주겠다.

관련 문제