UniformGrid에 ItemsSource를 사용하려면?

2025. 1. 3. 19:18· WPF
목차
  1. ✅ UniformGrid에 ItemsSource 달기
  2. View
  3. ViewModel
  4. 마무리
  5. ✅ UniformGrid에 ItemsSource 달기 - 끝

UniformGrid는 유용한 컨트롤이지만 Grid나 StackPanel보다는 상대적으로 덜 쓰이는 컨트롤이죠. 잘사용하면 정말 편리합니다. 특히 Dashboard를 구현할 때 편리합니다. 그런데 StackPanel과 마찬가지로 ItemsSource가 없기 때문에 다른 방법을 사용해줘야합니다. 사실 StackPanel과 같은 방법입니다.

 

 

✅ UniformGrid에 ItemsSource 달기

 

서두에 말씀드린 것 처럼 StackPanel과 같은 방식으로 하면 됩니다.

ItemsControl을 사용하는 것이죠.

 

 

View

<Window x:Class="UniformGridBindingExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="UniformGrid Binding Example" Height="450" Width="800">
    <Window.DataContext>        
        <local:MainViewModel />
    </Window.DataContext>
    <Grid>
        <ItemsControl ItemsSource="{Binding Items}">
            <ItemsControl.ItemsPanel>                
                <ItemsPanelTemplate>
                    <UniformGrid Rows="3" Columns="4" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>                
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="1" Margin="5">
                        <TextBlock Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

 

 

 

ViewModel

using System.Collections.ObjectModel;

namespace UniformGridBindingExample
{
    public class MainViewModel
    {
        public ObservableCollection<string> Items { get; set; }

        public MainViewModel()
        {
            Items = new ObservableCollection<string>
            {
                "Item 1",
                "Item 2",
                "Item 3",
                "Item 4",
                "Item 5",
                "Item 6",
                "Item 7",
                "Item 8"
            };
        }
    }
}

 

 

 

마무리

 

  1. ItemsControl은 ItemsSource를 통해 Items 컬렉션을 바인딩.
  2. 각 아이템은 DataTemplate에 정의된 방식으로 표시됨.
  3. UniformGrid는 컬렉션 항목을 자동으로 레이아웃에 배치.

이렇게 하면 UniformGrid에 ItemsSource를 사용할 수 있게 됩니다.

 

 

 

 

 

✅ UniformGrid에 ItemsSource 달기 - 끝

 

  1. ✅ UniformGrid에 ItemsSource 달기
  2. View
  3. ViewModel
  4. 마무리
  5. ✅ UniformGrid에 ItemsSource 달기 - 끝
'WPF' 카테고리의 다른 글
  • WPF UI v4.0 후기 (UI 라이브러리)
  • WPF에서 Font를 포함 배포하는 방법
  • WPF - GMap
  • IDialogService - ViewModel에서 Dialog Popup
YUNYUN3915
YUNYUN3915

공지사항

  • 블로그 이전 취소

인기 글

태그

  • Expanding Event
  • WPF 흰색바
  • GIT
  • 메세지팝업
  • CS8602
  • C#
  • WPF style
  • DialogService
  • 어트리뷰트
  • WindowsFormsHostingWpfControl
  • IDialogService
  • wpf 폰트
  • wpf
  • DataGrid
  • ItemsControl
  • itemssource
  • Git취소
  • ElementHost
  • command
  • PasswordBox MVVM
  • IValueConverter
  • RelayCommand
  • TreeView
  • ICloneable
  • TreeViewItem
  • PasswordBox DataBinding
  • OnPropertyChanged
  • 문자열 관리
  • wpf 전역 스타일
  • 윈도우탐색기
hELLO · Designed By 정상우.v4.2.0
YUNYUN3915
UniformGrid에 ItemsSource를 사용하려면?
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.