回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,106 +1,106 @@
|
|
1
|
-
`ListBox`は通常縦に並んだアイテムを選択するコントロールですが、あくまでデフォルトがそういう見た目なだけであって、WPFでは見た目を柔軟にカスタマイズできます。
|
2
|
-
|
3
|
-
前回の回答でも紹介していますが↓の`ItemsPanel`のところを見てください。
|
4
|
-
[ItemsControl 攻略 ~ 外観のカスタマイズ | grabacr.nét](http://grabacr.net/archives/1240#ItemsPanel)
|
5
|
-
|
6
|
-
コレクション中の**個々のアイテムの並べ方**をカスタマイズするテンプレートです。
|
7
|
-
|
8
|
-
指定できるのは[Panel](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.panel)クラスの派生クラスです。
|
9
|
-
|
10
|
-
[Canvas](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.canvas)・[Grid](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.grid)・[UniformGrid](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.primitives.uniformgrid)・[StackPanel](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.stackpanel)([VirtualizingStackPanel](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.virtualizingstackpanel))・[WrapPanel](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.wrappanel)あたりでしょうか。
|
11
|
-
|
12
|
-
前回の回答で`UniformGrid`を使ったのは、カレンダーは列数が固定・個々のアイテムサイズが同じという点がぴったりだったためです。
|
13
|
-
|
14
|
-
今回の回答でも`UniformGrid`を使用しましたが、(用途が合えば)大変便利なのですがあまり知られていないような気がします。
|
15
|
-
|
16
|
-
`Canvas`や`Grid`は何もしないとすべて重なってしまうので、あまり`ItemsPanel`向きとは言えないですね。
|
17
|
-
|
18
|
-
しかし実際に使うかは別としてこういった`ListBox`も作成可能です。
|
19
|
-
[ListBoxをカスタマイズして都道府県の地図を選択するUIを作成する - Yamakiの日記](http://yamaki.hatenablog.com/entry/20071011/1192091886)
|
20
|
-
|
21
|
-
```
|
22
|
-
<Window
|
23
|
-
x:Class="Questions337152.MainWindow"
|
24
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
25
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
26
|
-
Width="800"
|
27
|
-
Height="450">
|
28
|
-
<Window.Resources>
|
29
|
-
<!-- サンプルデータ 特に気にする必要はない -->
|
30
|
-
<ObjectDataProvider
|
31
|
-
xmlns:linq="clr-namespace:System.Linq;assembly=System.Core"
|
32
|
-
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
33
|
-
x:Key="One2Hundred"
|
34
|
-
MethodName="Range"
|
35
|
-
ObjectType="{x:Type linq:Enumerable}">
|
36
|
-
<ObjectDataProvider.MethodParameters>
|
37
|
-
<sys:Int32>1</sys:Int32>
|
38
|
-
<sys:Int32>100</sys:Int32>
|
39
|
-
</ObjectDataProvider.MethodParameters>
|
40
|
-
</ObjectDataProvider>
|
41
|
-
</Window.Resources>
|
42
|
-
|
43
|
-
<UniformGrid Columns="3">
|
44
|
-
|
45
|
-
<GroupBox Header="Nomal(StackPanel Vertical)">
|
46
|
-
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}" />
|
47
|
-
</GroupBox>
|
48
|
-
|
49
|
-
<GroupBox Header="StackPanel Horizontal">
|
50
|
-
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}">
|
51
|
-
<ListBox.ItemsPanel>
|
52
|
-
<ItemsPanelTemplate>
|
53
|
-
<StackPanel Orientation="Horizontal" />
|
54
|
-
</ItemsPanelTemplate>
|
55
|
-
</ListBox.ItemsPanel>
|
56
|
-
</ListBox>
|
57
|
-
</GroupBox>
|
58
|
-
|
59
|
-
<GroupBox Header="UniformGrid">
|
60
|
-
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}">
|
61
|
-
<ListBox.ItemsPanel>
|
62
|
-
<ItemsPanelTemplate>
|
63
|
-
<UniformGrid Columns="5" />
|
64
|
-
</ItemsPanelTemplate>
|
65
|
-
</ListBox.ItemsPanel>
|
66
|
-
</ListBox>
|
67
|
-
</GroupBox>
|
68
|
-
|
69
|
-
<GroupBox Header="WrapPanel Horizontal">
|
70
|
-
<!-- 横スクロールを規制しないと横に伸びきってしまって、StackPanel Horizontalと同じになってしまう -->
|
71
|
-
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
|
72
|
-
<ListBox.ItemsPanel>
|
73
|
-
<ItemsPanelTemplate>
|
74
|
-
<WrapPanel />
|
75
|
-
</ItemsPanelTemplate>
|
76
|
-
</ListBox.ItemsPanel>
|
77
|
-
</ListBox>
|
78
|
-
</GroupBox>
|
79
|
-
|
80
|
-
<GroupBox Header="WrapPanel Vertical">
|
81
|
-
<!-- 縦スクロールを規制しないと縦に伸びきってしまって、StackPanel Verticalと同じになってしまう -->
|
82
|
-
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
|
83
|
-
<ListBox.ItemsPanel>
|
84
|
-
<ItemsPanelTemplate>
|
85
|
-
<WrapPanel Orientation="Vertical" />
|
86
|
-
</ItemsPanelTemplate>
|
87
|
-
</ListBox.ItemsPanel>
|
88
|
-
</ListBox>
|
89
|
-
</GroupBox>
|
90
|
-
|
91
|
-
<GroupBox Header="Grid">
|
92
|
-
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}">
|
93
|
-
<ListBox.ItemsPanel>
|
94
|
-
<ItemsPanelTemplate>
|
95
|
-
<Grid />
|
96
|
-
</ItemsPanelTemplate>
|
97
|
-
</ListBox.ItemsPanel>
|
98
|
-
</ListBox>
|
99
|
-
</GroupBox>
|
100
|
-
|
101
|
-
</UniformGrid>
|
102
|
-
</Window>
|
103
|
-
```
|
104
|
-
|
105
|
-

|
1
|
+
`ListBox`は通常縦に並んだアイテムを選択するコントロールですが、あくまでデフォルトがそういう見た目なだけであって、WPFでは見た目を柔軟にカスタマイズできます。
|
2
|
+
|
3
|
+
前回の回答でも紹介していますが↓の`ItemsPanel`のところを見てください。
|
4
|
+
[ItemsControl 攻略 ~ 外観のカスタマイズ | grabacr.nét](http://grabacr.net/archives/1240#ItemsPanel)
|
5
|
+
|
6
|
+
コレクション中の**個々のアイテムの並べ方**をカスタマイズするテンプレートです。
|
7
|
+
|
8
|
+
指定できるのは[Panel](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.panel)クラスの派生クラスです。
|
9
|
+
|
10
|
+
[Canvas](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.canvas)・[Grid](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.grid)・[UniformGrid](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.primitives.uniformgrid)・[StackPanel](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.stackpanel)([VirtualizingStackPanel](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.virtualizingstackpanel))・[WrapPanel](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.wrappanel)あたりでしょうか。
|
11
|
+
|
12
|
+
前回の回答で`UniformGrid`を使ったのは、カレンダーは列数が固定・個々のアイテムサイズが同じという点がぴったりだったためです。
|
13
|
+
|
14
|
+
今回の回答でも`UniformGrid`を使用しましたが、(用途が合えば)大変便利なのですがあまり知られていないような気がします。
|
15
|
+
|
16
|
+
`Canvas`や`Grid`は何もしないとすべて重なってしまうので、あまり`ItemsPanel`向きとは言えないですね。
|
17
|
+
|
18
|
+
しかし実際に使うかは別としてこういった`ListBox`も作成可能です。
|
19
|
+
[ListBoxをカスタマイズして都道府県の地図を選択するUIを作成する - Yamakiの日記](http://yamaki.hatenablog.com/entry/20071011/1192091886)
|
20
|
+
|
21
|
+
```xml
|
22
|
+
<Window
|
23
|
+
x:Class="Questions337152.MainWindow"
|
24
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
25
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
26
|
+
Width="800"
|
27
|
+
Height="450">
|
28
|
+
<Window.Resources>
|
29
|
+
<!-- サンプルデータ 特に気にする必要はない -->
|
30
|
+
<ObjectDataProvider
|
31
|
+
xmlns:linq="clr-namespace:System.Linq;assembly=System.Core"
|
32
|
+
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
33
|
+
x:Key="One2Hundred"
|
34
|
+
MethodName="Range"
|
35
|
+
ObjectType="{x:Type linq:Enumerable}">
|
36
|
+
<ObjectDataProvider.MethodParameters>
|
37
|
+
<sys:Int32>1</sys:Int32>
|
38
|
+
<sys:Int32>100</sys:Int32>
|
39
|
+
</ObjectDataProvider.MethodParameters>
|
40
|
+
</ObjectDataProvider>
|
41
|
+
</Window.Resources>
|
42
|
+
|
43
|
+
<UniformGrid Columns="3">
|
44
|
+
|
45
|
+
<GroupBox Header="Nomal(StackPanel Vertical)">
|
46
|
+
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}" />
|
47
|
+
</GroupBox>
|
48
|
+
|
49
|
+
<GroupBox Header="StackPanel Horizontal">
|
50
|
+
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}">
|
51
|
+
<ListBox.ItemsPanel>
|
52
|
+
<ItemsPanelTemplate>
|
53
|
+
<StackPanel Orientation="Horizontal" />
|
54
|
+
</ItemsPanelTemplate>
|
55
|
+
</ListBox.ItemsPanel>
|
56
|
+
</ListBox>
|
57
|
+
</GroupBox>
|
58
|
+
|
59
|
+
<GroupBox Header="UniformGrid">
|
60
|
+
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}">
|
61
|
+
<ListBox.ItemsPanel>
|
62
|
+
<ItemsPanelTemplate>
|
63
|
+
<UniformGrid Columns="5" />
|
64
|
+
</ItemsPanelTemplate>
|
65
|
+
</ListBox.ItemsPanel>
|
66
|
+
</ListBox>
|
67
|
+
</GroupBox>
|
68
|
+
|
69
|
+
<GroupBox Header="WrapPanel Horizontal">
|
70
|
+
<!-- 横スクロールを規制しないと横に伸びきってしまって、StackPanel Horizontalと同じになってしまう -->
|
71
|
+
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
|
72
|
+
<ListBox.ItemsPanel>
|
73
|
+
<ItemsPanelTemplate>
|
74
|
+
<WrapPanel />
|
75
|
+
</ItemsPanelTemplate>
|
76
|
+
</ListBox.ItemsPanel>
|
77
|
+
</ListBox>
|
78
|
+
</GroupBox>
|
79
|
+
|
80
|
+
<GroupBox Header="WrapPanel Vertical">
|
81
|
+
<!-- 縦スクロールを規制しないと縦に伸びきってしまって、StackPanel Verticalと同じになってしまう -->
|
82
|
+
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
|
83
|
+
<ListBox.ItemsPanel>
|
84
|
+
<ItemsPanelTemplate>
|
85
|
+
<WrapPanel Orientation="Vertical" />
|
86
|
+
</ItemsPanelTemplate>
|
87
|
+
</ListBox.ItemsPanel>
|
88
|
+
</ListBox>
|
89
|
+
</GroupBox>
|
90
|
+
|
91
|
+
<GroupBox Header="Grid">
|
92
|
+
<ListBox ItemsSource="{Binding Source={StaticResource One2Hundred}}">
|
93
|
+
<ListBox.ItemsPanel>
|
94
|
+
<ItemsPanelTemplate>
|
95
|
+
<Grid />
|
96
|
+
</ItemsPanelTemplate>
|
97
|
+
</ListBox.ItemsPanel>
|
98
|
+
</ListBox>
|
99
|
+
</GroupBox>
|
100
|
+
|
101
|
+
</UniformGrid>
|
102
|
+
</Window>
|
103
|
+
```
|
104
|
+
|
105
|
+

|
106
106
|

|