programing

WPF ListBox의 ItemTemplate와 ItemContainerStyle의 차이점은 무엇입니까?

sourcejob 2023. 4. 24. 23:09
반응형

WPF ListBox의 ItemTemplate와 ItemContainerStyle의 차이점은 무엇입니까?

WPF의 경우Listbox다음 두 가지 개념과 혼동됩니다.ItemTemplate그리고.ItemContainerStyle누가 좀 더 설명해 주시겠어요?

ItemTemplate는 데이터 항목의 내용이 표시되는 방식을 스타일링하기 위한 것입니다.데이터 필드를 바인딩하거나 표시 문자열을 포맷하는 데 사용합니다.데이터가 어떻게 표시되는지 결정합니다.

ItemContainerStyle은 데이터 항목의 컨테이너 스타일링을 위한 것입니다.목록 상자에서 이것은 ListBoxItem이 됩니다.여기서 스타일링은 선택 동작이나 배경색 등에 영향을 줍니다.디스플레이의 스타일과 UX를 결정합니다.

위에 링크된 Item Container Style의 MSDN 페이지에는 몇 가지 차이점이 있는 좋은 예가 있습니다.

 <!--Use the ItemTemplate to set a DataTemplate to define
      the visualization of the data objects. This DataTemplate
      specifies that each data object appears with the Proriity
      and TaskName on top of a silver ellipse.-->
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <DataTemplate.Resources>
        <Style TargetType="TextBlock">
          <Setter Property="FontSize" Value="18"/>
          <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
      </DataTemplate.Resources>
      <Grid>
        <Ellipse Fill="Silver"/>
        <StackPanel>
          <TextBlock Margin="3,3,3,0"
                     Text="{Binding Path=Priority}"/>
          <TextBlock Margin="3,0,3,7"
                     Text="{Binding Path=TaskName}"/>
        </StackPanel>
      </Grid>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
  <!--Use the ItemContainerStyle property to specify the appearance
      of the element that contains the data. This ItemContainerStyle
      gives each item container a margin and a width. There is also
      a trigger that sets a tooltip that shows the description of
      the data object when the mouse hovers over the item container.-->
  <ItemsControl.ItemContainerStyle>
    <Style>
      <Setter Property="Control.Width" Value="100"/>
      <Setter Property="Control.Margin" Value="5"/>
      <Style.Triggers>
        <Trigger Property="Control.IsMouseOver" Value="True">
          <Setter Property="Control.ToolTip"
                  Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                          Path=Content.Description}"/>
        </Trigger>
      </Style.Triggers>
    </Style>
  </ItemsControl.ItemContainerStyle>

ItemContainerStyle은 DataTemplate의 래퍼일 뿐이므로 공통 항목 스타일을 다른 데이터 레이아웃에 적용할 수 있습니다.

또한답변에서 "DataTemplate vs ItemContainerStyle"에 대해:

ItemTemplate에서 모든 스타일링을 수행할 수 있지만 ItemContentStyle에는 마우스 오버/비활성화/선택 시 불투명도를 제어하는 VisualStates가 있습니다.

이러한 불투명도 상태 변경을 변경하거나 삼각형과 같이 직사각형 이외의 컨테이너 모양을 원하는 경우 기본 ItemContainerStyle을 재정의해야 합니다.

언급URL : https://stackoverflow.com/questions/16546143/whats-the-difference-between-itemtemplate-and-itemcontainerstyle-in-a-wpf-listb

반응형