본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - ListBox 를 WrapPanel 처럼 사용하기 ( ItemsPanelTemplate )

728x90
반응형

 

 

 

 WPF & C# - ListBox 를 WrapPanel 처럼 사용하기 ( ItemsPanelTemplate  )

 

ListBox_ItemsPanelTemplate.zip
0.79MB

 

 

MainWindow.xaml

 

 
1
2
3
4
5
6
7
8
<ListBox x:Name="lbx" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <Button x:Name="btn" Content="Button" Width="75" Height="67" Click="btn_Click"/>
</ListBox>
cs
 

 

코드로 넣기

MainWindow.xaml.cs

 

 
1
2
3
4
5
6
7
8
9
10
11
// 리스트박스 추가
ListBox lbx = new ListBox();
 
FrameworkElementFactory factoryPanel = new FrameworkElementFactory(typeof(WrapPanel));
factoryPanel.SetValue(WrapPanel.OrientationProperty, Orientation.Horizontal);
ItemsPanelTemplate template = new ItemsPanelTemplate();
template.VisualTree = factoryPanel;
lbx.ItemsPanel = template;
 
lbx.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);
lbx.ItemsPanel = template;
cs
 

 

 

ItemsPanelTemplate.zip
0.27MB

 

MainWindow.xaml.cs

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 컬러 종류 나열
private System.Windows.Media.Color[] circleColors =
{
    Colors.Red,
    Colors.Black,
    Colors.LawnGreen,
    Colors.Blue,
    Colors.Cyan,
    Colors.Magenta,
    Colors.Blue,
    Colors.BlueViolet,
    Colors.Brown,
    Colors.BurlyWood,
    Colors.Chartreuse,
    Colors.Coral,
    Colors.Tomato,
    Colors.Teal,
    Colors.SteelBlue,
    Colors.SpringGreen,
    Colors.SlateGray,
    Colors.SlateBlue,
    Colors.Purple,
    Colors.GreenYellow,
    Colors.Green,
    Colors.Firebrick,
    Colors.DarkOrchid,
    Colors.Crimson,
    Colors.Chartreuse,
    Colors.DarkViolet,
    Colors.DeepPink,
    Colors.Violet,
    Colors.YellowGreen,
    Colors.Yellow
};
 
int i = 0;
private void btn_Click(object sender, RoutedEventArgs e)
{
    // Ellipse 생성 
    Ellipse ell = new Ellipse();
    ell.Width = 50;
    ell.Height = 50;
    ell.Fill = new SolidColorBrush(circleColors[i++ % circleColors.Count()]);
 
    lbx.Items.Add(ell);
}
cs
 

 

 

 

728x90
반응형