Home C#WPF [WPF] ItemsPresenter

[WPF] ItemsPresenter

by DUBUKIMCH

ItemsPresenter는 WPF에서 ItemsControl 내부에서 사용되는 클래스입니다. 주로 ItemsControl의 템플릿에서 정의되며, ItemsControlItemsPanel에 지정된 레이아웃 컨테이너(Panel)를 표시하는 역할을 합니다.

ItemsPresenterItemsControl의 자식 요소를 정확히 어디에 렌더링할지를 결정하는 데 사용됩니다. 이는 ControlTemplate과 함께 작동하여 사용자 정의 UI를 만드는 데 유용합니다.

주요 특징

  1. ItemsControl와 연동: ItemsControlItemsSource로부터 데이터를 가져와 ItemsPanel에 배치합니다.
  2. 템플릿 확장성: ControlTemplate에서 ItemsPresenter를 명시적으로 지정해 원하는 디자인과 레이아웃을 구현할 수 있습니다.
  3. 데이터 바인딩 지원: ItemsPresenterItemsControl의 데이터 항목과 자동으로 연결됩니다.

활용 예시

기본 ItemsControl 예시

기본적으로 ItemsControl은 내부적으로 ItemsPresenter를 사용합니다. ItemsControl의 기본 템플릿에는 ItemsPresenter가 포함되어 있습니다.

MainWindow.xaml

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ItemsPresenter Example" Height="350" Width="525">
    <Grid>
        <ItemsControl Name="myItemsControl" HorizontalAlignment="Center" VerticalAlignment="Center">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>
</Window>

MainWindow.xaml.cs

// MainWindow.xaml.cs
using System.Windows;

namespace WpfApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            myItemsControl.Items.Add("Item 1");
            myItemsControl.Items.Add("Item 2");
            myItemsControl.Items.Add("Item 3");
        }
    }
}

실행 결과

사용자 정의 템플릿에서 ItemsPresenter 활용

ControlTemplate에서 ItemsPresenter를 명시적으로 사용할 수 있습니다.

MainWindow.xaml

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ItemsPresenter Example" Height="350" Width="525">
    <Grid>
        <ItemsControl Name="myItemsControl" HorizontalAlignment="Center" VerticalAlignment="Center">
            <ItemsControl.Template>
                <ControlTemplate TargetType="ItemsControl">
                    <Border BorderBrush="Black" BorderThickness="2" Padding="10">
                        <!-- ItemsPresenter를 명시적으로 지정 -->
                        <ItemsPresenter />
                    </Border>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>
</Window>

MainWindow.xaml.cs

// MainWindow.xaml.cs
using System.Windows;

namespace WpfApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            myItemsControl.Items.Add("Item A");
            myItemsControl.Items.Add("Item B");
            myItemsControl.Items.Add("Item C");
        }
    }
}

실행 결과

    요약

    ItemsPresenterItemsControl의 데이터를 화면에 렌더링하는 데 중요한 역할을 하며, 이를 통해 데이터 템플릿을 사용자 정의하거나 특정 레이아웃 요구사항을 충족할 수 있습니다. ControlTemplate과 함께 사용하면 더욱 강력한 UI 커스터마이징을 구현할 수 있습니다.

    You may also like

    Leave a Comment

    error: Content is protected !!