2011-01-06 1 views
0

ItemContainerStyle을 지정하는 ListBox가 있습니다. ItemContainerStyle은 트리거를 사용하여 ContentTemplate을 설정합니다.템플릿을 적용 할 때 datatemplate 멤버의 가시성을 설정하는 방법은 무엇입니까?

템플릿을 적용 할 때 ContentTemplate에서 ContentControl ("ExpanderContent")의 가시성을 설정하고 싶습니다. 가능한 경우 ListBoxItem에 설정된 연결된 속성의 값을 사용하고 싶습니다.

아마도 ContentControl ("ExpanderContent")에 적용된 스타일을 사용하여 시도한이 예제를 더 명확하게 볼 수 있습니다. 스타일이 아래 코드의 ContentControl에 설정되어 있지 않다는 것을 알고 있습니다. 나는 그것을 설정하고 적용된 것을 보았고 첨부 된 속성을 해결하는 데 오류가 없음을 확인했습니다.

<ListBox ItemContainerStyle="{StaticResource lbcStyle}"/> 

<Style TargetType="ListBoxItem" x:Key="lbcStyle"> 
    <Style.Triggers> 
    <Trigger Property="IsSelected" Value="True"> 
     <Setter Property="ContentTemplate" Value="{StaticResource editable}"/> 
    </Trigger> 
    </Style.Triggers> 
    <Setter Property="ContentTemplate" Value="{StaticResource nonEditable}"/> 
</Style> 

<DataTemplate x:Key="nonEditable"> 
    <Grid Width="Auto" Height="Auto"> 
    ... 
    <ContentControl Name="ExpanderContent" Visibility="Collapsed" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="12"></ContentControl> 
    </Grid> 
</DataTemplate> 
<DataTemplate x:Key="editable"> 
    <Grid x:Name="grdEditable" Width="Auto" Height="Auto"> 
    ... 
    <ContentControl Name="ExpanderContent" Visibility="Collapsed" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="12"></ContentControl> 
    </Grid> 
</DataTemplate> 

<Style x:Key="editorContentControl" TargetType="{x:Type ContentControl}"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding (local:AttachedProperties.IsExpanded),RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}" Value="True"> 
      <Setter Property="Visibility" Value="Visible"/> 
     </DataTrigger> 
     <DataTrigger Binding="{Binding (local:AttachedProperties.IsExpanded),RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}" Value="False"> 
      <Setter Property="Visibility" Value="Collapsed"/> 
     </DataTrigger> 
    </Style.Triggers> 
    <Setter Property="Visibility" Value="Visible"/> 
</Style> 

이 넌센스의 실제 적용은 목록 상자가 있고 목록 상자의 항목에 선택하면 변경되는 템플릿이 있다는 것입니다. 템플릿에는 확장기가 있으며 항목을 선택하지 않아도 확장기를 확장 된 상태로 유지하고 싶습니다. 지금은 목록 상자 선택을 변경할 때마다 콘텐츠 템플릿이 원래 상태 인 붕괴되어 다시 적용되므로 한 번에 두 개 이상을 확장 할 수 없습니다.

alt text

답변

1

가장 간단한 해결책은, 오히려 다른 템플릿 사이에 주변에 시각적 상태 값을 전달하는 것보다 당신의 ContentTemplate에 대한 하나의 DataTemplate을 사용하고 그냥 템플릿에 트리거를 사용하여 표시하도록하고 적절한 컨트롤을 숨기 현재 선택 상태. 그렇게하면 상태를 전환 할 때마다 확장기 컨트롤을 바꾸지 않을 것입니다.

+0

사실 간단하지만 재 작성이 필요합니다. –