回答編集履歴
5
見直しキャンペーン中
test
CHANGED
@@ -1,315 +1,257 @@
|
|
1
1
|
いろいろな方法がありそうですが、ぱっと思いついたのはこんなところです。
|
2
2
|
|
3
|
-
|
4
|
-
|
5
3
|
非常にバカバカしい^^;
|
6
|
-
|
7
|
-
```x
|
4
|
+
```xml
|
8
|
-
|
9
5
|
<ListBox
|
10
|
-
|
11
6
|
x:Name="dummy"
|
12
|
-
|
13
7
|
Grid.Row="2"
|
14
|
-
|
15
8
|
Grid.RowSpan="5">
|
16
|
-
|
17
|
-
<TextBlock Margin="3" />
|
9
|
+
<TextBlock Margin="3" />
|
18
|
-
|
19
|
-
<TextBlock Margin="3" />
|
10
|
+
<TextBlock Margin="3" />
|
20
|
-
|
21
|
-
<TextBlock Margin="3" />
|
11
|
+
<TextBlock Margin="3" />
|
22
|
-
|
23
|
-
<TextBlock Margin="3" />
|
12
|
+
<TextBlock Margin="3" />
|
24
|
-
|
25
|
-
<TextBlock Margin="3" />
|
13
|
+
<TextBlock Margin="3" />
|
26
|
-
|
27
14
|
</ListBox>
|
28
|
-
|
29
15
|
<ListBox
|
30
|
-
|
31
16
|
x:Name="lstFamilyName"
|
32
|
-
|
33
17
|
Grid.Row="2"
|
34
|
-
|
35
18
|
Grid.RowSpan="5"
|
36
|
-
|
37
19
|
Height="{Binding ActualHeight, ElementName=dummy}">
|
38
|
-
|
39
20
|
</ListBox>
|
40
|
-
|
41
21
|
```
|
42
22
|
|
43
|
-
|
23
|
+
---
|
24
|
+
|
44
|
-
|
25
|
+
追記
|
26
|
+
`Height`でなく`MaxHeight`にすべきだった。
|
27
|
+
|
28
|
+
```xml
|
29
|
+
<Window
|
30
|
+
x:Class="Questions264300.MainWindow"
|
31
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
32
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
33
|
+
xmlns:local="clr-namespace:Questions264300"
|
34
|
+
Title="フォントの選択"
|
35
|
+
MinWidth="542"
|
36
|
+
ResizeMode="NoResize"
|
37
|
+
SizeToContent="WidthAndHeight"
|
38
|
+
WindowStyle="SingleBorderWindow">
|
39
|
+
<Window.Resources>
|
40
|
+
<local:Point2PixelConverter x:Key="point2PixelConverter" />
|
41
|
+
</Window.Resources>
|
42
|
+
|
43
|
+
<DockPanel
|
44
|
+
Margin="10"
|
45
|
+
TextBlock.FontFamily="{Binding SelectedValue, ElementName=lstFamilyName, Mode=TwoWay}"
|
46
|
+
TextBlock.FontSize="{Binding SelectedValue, ElementName=lstFontSize, Mode=TwoWay, Converter={StaticResource point2PixelConverter}}">
|
47
|
+
<Grid Margin="0,10,0,0" DockPanel.Dock="Bottom">
|
48
|
+
<Grid.ColumnDefinitions>
|
49
|
+
<ColumnDefinition Width="2*" />
|
50
|
+
<ColumnDefinition Width="1*" />
|
51
|
+
<ColumnDefinition Width="5" />
|
52
|
+
<ColumnDefinition Width="1*" />
|
53
|
+
</Grid.ColumnDefinitions>
|
54
|
+
|
55
|
+
<Button
|
56
|
+
Grid.Column="1"
|
57
|
+
Content="OK"
|
58
|
+
IsDefault="True" />
|
59
|
+
<Button
|
60
|
+
Grid.Column="3"
|
61
|
+
Content="キャンセル"
|
62
|
+
IsCancel="True" />
|
63
|
+
</Grid>
|
64
|
+
|
65
|
+
<Grid>
|
66
|
+
<Grid.ColumnDefinitions>
|
67
|
+
<ColumnDefinition Width="2.5*" />
|
68
|
+
<ColumnDefinition Width="5" />
|
69
|
+
<ColumnDefinition Width="2.5*" />
|
70
|
+
<ColumnDefinition Width="5" />
|
71
|
+
<ColumnDefinition Width="1*" />
|
72
|
+
</Grid.ColumnDefinitions>
|
73
|
+
<Grid.RowDefinitions>
|
74
|
+
<RowDefinition Height="Auto" />
|
75
|
+
<RowDefinition Height="Auto" />
|
76
|
+
<RowDefinition Height="Auto" />
|
77
|
+
<RowDefinition Height="Auto" />
|
78
|
+
<RowDefinition />
|
79
|
+
<RowDefinition Height="Auto" />
|
80
|
+
<RowDefinition Height="Auto" />
|
81
|
+
</Grid.RowDefinitions>
|
82
|
+
|
83
|
+
<Label Content="フォント(_F)" Target="{Binding ElementName=txtFamilyName}" />
|
84
|
+
<TextBox
|
85
|
+
x:Name="txtFamilyName"
|
86
|
+
Grid.Row="1"
|
87
|
+
FontWeight="Bold" />
|
88
|
+
|
89
|
+
<ListBox
|
90
|
+
x:Name="dummy"
|
91
|
+
Grid.Row="2"
|
92
|
+
Grid.RowSpan="5"
|
93
|
+
VerticalAlignment="Top"
|
94
|
+
Visibility="Hidden">
|
95
|
+
<TextBlock Margin="3" />
|
96
|
+
<TextBlock Margin="3" />
|
97
|
+
<TextBlock Margin="3" />
|
98
|
+
<TextBlock Margin="3" />
|
99
|
+
<TextBlock Margin="3" />
|
100
|
+
<TextBlock Margin="3" />
|
101
|
+
<TextBlock Margin="3" />
|
102
|
+
<TextBlock Margin="3" />
|
103
|
+
<TextBlock Margin="3" />
|
104
|
+
<TextBlock Margin="3" />
|
105
|
+
</ListBox>
|
106
|
+
<ListBox
|
107
|
+
x:Name="lstFamilyName"
|
108
|
+
Grid.Row="2"
|
109
|
+
Grid.RowSpan="5"
|
110
|
+
MaxHeight="{Binding ActualHeight, ElementName=dummy}"
|
111
|
+
ItemsSource="{x:Static local:SystemFontsModel.SystemFonts}">
|
112
|
+
<ListBox.ItemTemplate>
|
113
|
+
<DataTemplate>
|
114
|
+
<StackPanel Orientation="Horizontal">
|
115
|
+
<TextBlock
|
116
|
+
Margin="3"
|
117
|
+
VerticalAlignment="Center"
|
118
|
+
Text="{Binding}" />
|
119
|
+
<TextBlock
|
120
|
+
VerticalAlignment="Center"
|
121
|
+
FontFamily="{Binding}"
|
122
|
+
Text="Sample" />
|
123
|
+
</StackPanel>
|
124
|
+
</DataTemplate>
|
45
|
-
|
125
|
+
</ListBox.ItemTemplate>
|
46
|
-
|
126
|
+
</ListBox>
|
127
|
+
|
128
|
+
<Label
|
129
|
+
Grid.Column="2"
|
130
|
+
Content="タイプフェース(_Y)"
|
131
|
+
Target="{Binding ElementName=txtTypeface}" />
|
132
|
+
<TextBox
|
133
|
+
x:Name="txtTypeface"
|
134
|
+
Grid.Row="1"
|
135
|
+
Grid.Column="2"
|
136
|
+
FontWeight="Bold" />
|
137
|
+
<ListBox
|
138
|
+
x:Name="lstTypeface"
|
139
|
+
Grid.Row="2"
|
140
|
+
Grid.Column="2" />
|
141
|
+
<Label
|
142
|
+
Grid.Row="3"
|
143
|
+
Grid.Column="2"
|
144
|
+
Content="サンプル(_M)"
|
145
|
+
Target="{Binding ElementName=txtSample}" />
|
146
|
+
<TextBox
|
147
|
+
Name="txtSample"
|
148
|
+
Grid.Row="4"
|
149
|
+
Grid.Column="2"
|
150
|
+
Grid.ColumnSpan="3"
|
151
|
+
AcceptsReturn="True"
|
152
|
+
TextWrapping="Wrap"
|
153
|
+
ToolTip="テキストの編集可能" />
|
154
|
+
<Label
|
155
|
+
Grid.Row="5"
|
156
|
+
Grid.Column="2"
|
157
|
+
Content="言語(_L)"
|
158
|
+
Target="{Binding ElementName=cmbLanguage}" />
|
159
|
+
<ComboBox
|
160
|
+
x:Name="cmbLanguage"
|
161
|
+
Grid.Row="6"
|
162
|
+
Grid.Column="2"
|
163
|
+
Grid.ColumnSpan="3" />
|
164
|
+
|
165
|
+
<Label
|
166
|
+
Grid.Column="4"
|
167
|
+
Content="サイズ(_S)"
|
168
|
+
Target="{Binding ElementName=txtFontSize}" />
|
169
|
+
<TextBox
|
170
|
+
x:Name="txtFontSize"
|
171
|
+
Grid.Row="1"
|
172
|
+
Grid.Column="4"
|
173
|
+
FontWeight="Bold" />
|
174
|
+
|
175
|
+
<ListBox
|
176
|
+
x:Name="dummy2"
|
177
|
+
Grid.Row="2"
|
178
|
+
Grid.Column="4"
|
179
|
+
VerticalAlignment="Top"
|
180
|
+
Visibility="Hidden">
|
181
|
+
<ListBoxItem>0</ListBoxItem>
|
182
|
+
<ListBoxItem>0</ListBoxItem>
|
183
|
+
<ListBoxItem>0</ListBoxItem>
|
184
|
+
<ListBoxItem>0</ListBoxItem>
|
185
|
+
<ListBoxItem>0</ListBoxItem>
|
186
|
+
</ListBox>
|
187
|
+
<ListBox
|
188
|
+
x:Name="lstFontSize"
|
189
|
+
Grid.Row="2"
|
190
|
+
Grid.Column="4"
|
191
|
+
MaxHeight="{Binding ActualHeight, ElementName=dummy2}"
|
192
|
+
ItemsSource="{Binding Sizes}"
|
193
|
+
SelectedIndex="5" />
|
194
|
+
</Grid>
|
195
|
+
</DockPanel>
|
196
|
+
</Window>
|
197
|
+
```
|
198
|
+
|
47
|
-
```
|
199
|
+
```cs
|
48
|
-
|
200
|
+
using System;
|
49
|
-
|
201
|
+
using System.Collections.Generic;
|
50
|
-
|
202
|
+
using System.Globalization;
|
203
|
+
using System.Linq;
|
204
|
+
using System.Windows;
|
205
|
+
using System.Windows.Data;
|
206
|
+
using System.Windows.Markup;
|
207
|
+
using System.Windows.Media;
|
208
|
+
|
209
|
+
namespace Questions264300
|
51
210
|
{
|
52
|
-
|
211
|
+
public partial class MainWindow : Window
|
212
|
+
{
|
213
|
+
public int[] Sizes { get; } = { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72, };
|
214
|
+
|
215
|
+
public MainWindow()
|
216
|
+
{
|
217
|
+
InitializeComponent();
|
218
|
+
DataContext = this;
|
219
|
+
}
|
220
|
+
}
|
221
|
+
|
222
|
+
public class SystemFontsModel
|
223
|
+
{
|
224
|
+
private static ICollection<FontFamily> _SystemFonts;
|
225
|
+
public static ICollection<FontFamily> SystemFonts => _SystemFonts ??= GetSystemFonts();
|
226
|
+
|
227
|
+
private static ICollection<FontFamily> GetSystemFonts()
|
228
|
+
{
|
229
|
+
var systemFonts = new List<FontFamily>();
|
230
|
+
var jp = XmlLanguage.GetLanguage("ja-jp");
|
231
|
+
var us = XmlLanguage.GetLanguage("en-us");
|
232
|
+
foreach (var font in Fonts.SystemFontFamilies)
|
233
|
+
{
|
234
|
+
if (font.FamilyNames.ContainsKey(jp)) systemFonts.Add(new FontFamily(font.FamilyNames[jp]));
|
53
|
-
|
235
|
+
else if (font.FamilyNames.ContainsKey(us)) systemFonts.Add(new FontFamily(font.FamilyNames[us]));
|
54
|
-
|
236
|
+
else systemFonts.Add(new FontFamily(font.FamilyNames.First().Value));
|
237
|
+
}
|
238
|
+
systemFonts.Sort((x, y) => string.Compare(x.Source, y.Source, StringComparison.Ordinal));
|
239
|
+
return systemFonts;
|
240
|
+
}
|
241
|
+
}
|
242
|
+
|
243
|
+
public class Point2PixelConverter : IValueConverter
|
244
|
+
{
|
245
|
+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
55
|
-
|
246
|
+
=> value is int point ? point * (96.0 / 72.0) : DependencyProperty.UnsetValue;
|
56
|
-
|
247
|
+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
|
248
|
+
}
|
57
249
|
}
|
58
250
|
|
59
251
|
```
|
60
|
-
|
252
|
+
![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-22/027dd677-2626-4235-b078-b5be96f75aa8.png)
|
61
|
-
|
253
|
+
![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-22/28cf91f7-00b2-4ea1-b8f6-1d24bcae28fa.png)
|
62
254
|
|
63
255
|
---
|
64
256
|
|
65
|
-
|
66
|
-
|
67
|
-
|
257
|
+
ぶっちゃけこんな(選択したフォントサイズと連動する)ダイアログ絶対イヤですけどね(サンプル欄の意味って。。。)
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
---
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
追記 質問を読めてませんでした^^;
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
```xaml
|
80
|
-
|
81
|
-
<Window
|
82
|
-
|
83
|
-
x:Class="Questions264300.MainWindow"
|
84
|
-
|
85
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
86
|
-
|
87
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
88
|
-
|
89
|
-
Loaded="Window_Loaded" SizeToContent="WidthAndHeight">
|
90
|
-
|
91
|
-
<Window.Resources>
|
92
|
-
|
93
|
-
<DataTemplate x:Key="FontTemplate">
|
94
|
-
|
95
|
-
<StackPanel Orientation="Horizontal">
|
96
|
-
|
97
|
-
<TextBlock
|
98
|
-
|
99
|
-
Margin="3"
|
100
|
-
|
101
|
-
VerticalAlignment="Center"
|
102
|
-
|
103
|
-
Text="{Binding}" />
|
104
|
-
|
105
|
-
<TextBlock
|
106
|
-
|
107
|
-
VerticalAlignment="Center"
|
108
|
-
|
109
|
-
FontFamily="{Binding}"
|
110
|
-
|
111
|
-
Text="Sample" />
|
112
|
-
|
113
|
-
</StackPanel>
|
114
|
-
|
115
|
-
</DataTemplate>
|
116
|
-
|
117
|
-
</Window.Resources>
|
118
|
-
|
119
|
-
<StackPanel Orientation="Horizontal">
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
<ListBox
|
124
|
-
|
125
|
-
x:Name="lstFamilyName0"
|
126
|
-
|
127
|
-
VerticalAlignment="Top"
|
128
|
-
|
129
|
-
ItemTemplate="{StaticResource FontTemplate}" />
|
130
|
-
|
131
|
-
<ListBox
|
132
|
-
|
133
|
-
x:Name="lstFamilyName1"
|
134
|
-
|
135
|
-
VerticalAlignment="Top"
|
136
|
-
|
137
|
-
ItemTemplate="{StaticResource FontTemplate}" />
|
138
|
-
|
139
|
-
<ListBox
|
140
|
-
|
141
|
-
x:Name="lstFamilyName3"
|
142
|
-
|
143
|
-
VerticalAlignment="Top"
|
144
|
-
|
145
|
-
ItemTemplate="{StaticResource FontTemplate}" />
|
146
|
-
|
147
|
-
<ListBox
|
148
|
-
|
149
|
-
x:Name="lstFamilyName5"
|
150
|
-
|
151
|
-
VerticalAlignment="Top"
|
152
|
-
|
153
|
-
ItemTemplate="{StaticResource FontTemplate}" />
|
154
|
-
|
155
|
-
<ListBox
|
156
|
-
|
157
|
-
x:Name="lstFamilyName"
|
158
|
-
|
159
|
-
VerticalAlignment="Top"
|
160
|
-
|
161
|
-
ItemTemplate="{StaticResource FontTemplate}" />
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
</StackPanel>
|
166
|
-
|
167
|
-
</Window>
|
168
|
-
|
169
|
-
```
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
```C#
|
174
|
-
|
175
|
-
using System;
|
176
|
-
|
177
|
-
using System.Collections.Generic;
|
178
|
-
|
179
|
-
using System.Linq;
|
180
|
-
|
181
|
-
using System.Windows;
|
182
|
-
|
183
|
-
using System.Windows.Controls;
|
184
|
-
|
185
|
-
using System.Windows.Markup;
|
186
|
-
|
187
|
-
using System.Windows.Media;
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
namespace Questions264300
|
192
|
-
|
193
|
-
{
|
194
|
-
|
195
|
-
public partial class MainWindow : Window
|
196
|
-
|
197
|
-
{
|
198
|
-
|
199
|
-
public MainWindow()
|
200
|
-
|
201
|
-
{
|
202
|
-
|
203
|
-
InitializeComponent();
|
204
|
-
|
205
|
-
lstFamilyName0.ItemsSource = SystemFontsModel.SystemFonts.Take(0);
|
206
|
-
|
207
|
-
lstFamilyName1.ItemsSource = SystemFontsModel.SystemFonts.Take(1);
|
208
|
-
|
209
|
-
lstFamilyName3.ItemsSource = SystemFontsModel.SystemFonts.Take(3);
|
210
|
-
|
211
|
-
lstFamilyName5.ItemsSource = SystemFontsModel.SystemFonts.Take(5);
|
212
|
-
|
213
|
-
lstFamilyName.ItemsSource = SystemFontsModel.SystemFonts;
|
214
|
-
|
215
|
-
}
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
private void Window_Loaded(object sender, RoutedEventArgs e)
|
220
|
-
|
221
|
-
{
|
222
|
-
|
223
|
-
{
|
224
|
-
|
225
|
-
var listBoxItem = lstFamilyName0.ItemContainerGenerator.ContainerFromItem(lstFamilyName0.ItemsSource.Cast<object>().FirstOrDefault()) as ListBoxItem;
|
226
|
-
|
227
|
-
lstFamilyName0.MaxHeight = (listBoxItem?.ActualHeight * 5 + 4) ?? 0; // 4=Border Padding BorderThickness分
|
228
|
-
|
229
|
-
}
|
230
|
-
|
231
|
-
{
|
232
|
-
|
233
|
-
var listBoxItem = lstFamilyName1.ItemContainerGenerator.ContainerFromItem(lstFamilyName1.ItemsSource.Cast<object>().FirstOrDefault()) as ListBoxItem;
|
234
|
-
|
235
|
-
lstFamilyName1.MaxHeight = (listBoxItem?.ActualHeight * 5 + 4) ?? 0; // 4=Border Padding BorderThickness分
|
236
|
-
|
237
|
-
}
|
238
|
-
|
239
|
-
{
|
240
|
-
|
241
|
-
var listBoxItem = lstFamilyName3.ItemContainerGenerator.ContainerFromItem(lstFamilyName3.ItemsSource.Cast<object>().FirstOrDefault()) as ListBoxItem;
|
242
|
-
|
243
|
-
lstFamilyName3.MaxHeight = (listBoxItem?.ActualHeight * 5 + 4) ?? 0; // 4=Border Padding BorderThickness分
|
244
|
-
|
245
|
-
}
|
246
|
-
|
247
|
-
{
|
248
|
-
|
249
|
-
var listBoxItem = lstFamilyName5.ItemContainerGenerator.ContainerFromItem(lstFamilyName5.ItemsSource.Cast<object>().FirstOrDefault()) as ListBoxItem;
|
250
|
-
|
251
|
-
lstFamilyName5.MaxHeight = (listBoxItem?.ActualHeight * 5 + 4) ?? 0; // 4=Border Padding BorderThickness分
|
252
|
-
|
253
|
-
}
|
254
|
-
|
255
|
-
{
|
256
|
-
|
257
|
-
var listBoxItem = lstFamilyName.ItemContainerGenerator.ContainerFromItem(lstFamilyName.ItemsSource.Cast<object>().FirstOrDefault()) as ListBoxItem;
|
258
|
-
|
259
|
-
lstFamilyName.MaxHeight = (listBoxItem?.ActualHeight * 5 + 4) ?? 0; // 4=Border Padding BorderThickness分
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
class SystemFontsModel
|
268
|
-
|
269
|
-
{
|
270
|
-
|
271
|
-
private static ICollection<FontFamily> _SystemFonts;
|
272
|
-
|
273
|
-
public static ICollection<FontFamily> SystemFonts => _SystemFonts ??= GetSystemFonts();
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
private static ICollection<FontFamily> GetSystemFonts()
|
278
|
-
|
279
|
-
{
|
280
|
-
|
281
|
-
var systemFonts = new List<FontFamily>();
|
282
|
-
|
283
|
-
var jp = XmlLanguage.GetLanguage("ja-jp");
|
284
|
-
|
285
|
-
var us = XmlLanguage.GetLanguage("en-us");
|
286
|
-
|
287
|
-
foreach(var font in Fonts.SystemFontFamilies)
|
288
|
-
|
289
|
-
{
|
290
|
-
|
291
|
-
if(font.FamilyNames.ContainsKey(jp)) systemFonts.Add(new FontFamily(font.FamilyNames[jp]));
|
292
|
-
|
293
|
-
else if(font.FamilyNames.ContainsKey(us)) systemFonts.Add(new FontFamily(font.FamilyNames[us]));
|
294
|
-
|
295
|
-
else systemFonts.Add(new FontFamily(font.FamilyNames.First().Value));
|
296
|
-
|
297
|
-
}
|
298
|
-
|
299
|
-
systemFonts.Sort((x, y) => string.Compare(x.Source, y.Source, StringComparison.Ordinal));
|
300
|
-
|
301
|
-
return systemFonts;
|
302
|
-
|
303
|
-
}
|
304
|
-
|
305
|
-
}
|
306
|
-
|
307
|
-
}
|
308
|
-
|
309
|
-
}
|
310
|
-
|
311
|
-
```
|
312
|
-
|
313
|
-
![アプリ画像](383a253147c4a02b7d63b9e94837986c.png)
|
314
|
-
|
315
|
-
多分ほかのリストボックスにも適用するんでしょうから、ビヘイビアのほうがいいでしょうね^^;
|
4
質問を読めてませんでした^^;
test
CHANGED
@@ -65,3 +65,251 @@
|
|
65
65
|
|
66
66
|
|
67
67
|
WPFに標準でフォントダイアログがあればこんな苦労いらないのですがね^^;(私も作りました)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
---
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
追記 質問を読めてませんでした^^;
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
```xaml
|
80
|
+
|
81
|
+
<Window
|
82
|
+
|
83
|
+
x:Class="Questions264300.MainWindow"
|
84
|
+
|
85
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
86
|
+
|
87
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
88
|
+
|
89
|
+
Loaded="Window_Loaded" SizeToContent="WidthAndHeight">
|
90
|
+
|
91
|
+
<Window.Resources>
|
92
|
+
|
93
|
+
<DataTemplate x:Key="FontTemplate">
|
94
|
+
|
95
|
+
<StackPanel Orientation="Horizontal">
|
96
|
+
|
97
|
+
<TextBlock
|
98
|
+
|
99
|
+
Margin="3"
|
100
|
+
|
101
|
+
VerticalAlignment="Center"
|
102
|
+
|
103
|
+
Text="{Binding}" />
|
104
|
+
|
105
|
+
<TextBlock
|
106
|
+
|
107
|
+
VerticalAlignment="Center"
|
108
|
+
|
109
|
+
FontFamily="{Binding}"
|
110
|
+
|
111
|
+
Text="Sample" />
|
112
|
+
|
113
|
+
</StackPanel>
|
114
|
+
|
115
|
+
</DataTemplate>
|
116
|
+
|
117
|
+
</Window.Resources>
|
118
|
+
|
119
|
+
<StackPanel Orientation="Horizontal">
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
<ListBox
|
124
|
+
|
125
|
+
x:Name="lstFamilyName0"
|
126
|
+
|
127
|
+
VerticalAlignment="Top"
|
128
|
+
|
129
|
+
ItemTemplate="{StaticResource FontTemplate}" />
|
130
|
+
|
131
|
+
<ListBox
|
132
|
+
|
133
|
+
x:Name="lstFamilyName1"
|
134
|
+
|
135
|
+
VerticalAlignment="Top"
|
136
|
+
|
137
|
+
ItemTemplate="{StaticResource FontTemplate}" />
|
138
|
+
|
139
|
+
<ListBox
|
140
|
+
|
141
|
+
x:Name="lstFamilyName3"
|
142
|
+
|
143
|
+
VerticalAlignment="Top"
|
144
|
+
|
145
|
+
ItemTemplate="{StaticResource FontTemplate}" />
|
146
|
+
|
147
|
+
<ListBox
|
148
|
+
|
149
|
+
x:Name="lstFamilyName5"
|
150
|
+
|
151
|
+
VerticalAlignment="Top"
|
152
|
+
|
153
|
+
ItemTemplate="{StaticResource FontTemplate}" />
|
154
|
+
|
155
|
+
<ListBox
|
156
|
+
|
157
|
+
x:Name="lstFamilyName"
|
158
|
+
|
159
|
+
VerticalAlignment="Top"
|
160
|
+
|
161
|
+
ItemTemplate="{StaticResource FontTemplate}" />
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
</StackPanel>
|
166
|
+
|
167
|
+
</Window>
|
168
|
+
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
```C#
|
174
|
+
|
175
|
+
using System;
|
176
|
+
|
177
|
+
using System.Collections.Generic;
|
178
|
+
|
179
|
+
using System.Linq;
|
180
|
+
|
181
|
+
using System.Windows;
|
182
|
+
|
183
|
+
using System.Windows.Controls;
|
184
|
+
|
185
|
+
using System.Windows.Markup;
|
186
|
+
|
187
|
+
using System.Windows.Media;
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
namespace Questions264300
|
192
|
+
|
193
|
+
{
|
194
|
+
|
195
|
+
public partial class MainWindow : Window
|
196
|
+
|
197
|
+
{
|
198
|
+
|
199
|
+
public MainWindow()
|
200
|
+
|
201
|
+
{
|
202
|
+
|
203
|
+
InitializeComponent();
|
204
|
+
|
205
|
+
lstFamilyName0.ItemsSource = SystemFontsModel.SystemFonts.Take(0);
|
206
|
+
|
207
|
+
lstFamilyName1.ItemsSource = SystemFontsModel.SystemFonts.Take(1);
|
208
|
+
|
209
|
+
lstFamilyName3.ItemsSource = SystemFontsModel.SystemFonts.Take(3);
|
210
|
+
|
211
|
+
lstFamilyName5.ItemsSource = SystemFontsModel.SystemFonts.Take(5);
|
212
|
+
|
213
|
+
lstFamilyName.ItemsSource = SystemFontsModel.SystemFonts;
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
private void Window_Loaded(object sender, RoutedEventArgs e)
|
220
|
+
|
221
|
+
{
|
222
|
+
|
223
|
+
{
|
224
|
+
|
225
|
+
var listBoxItem = lstFamilyName0.ItemContainerGenerator.ContainerFromItem(lstFamilyName0.ItemsSource.Cast<object>().FirstOrDefault()) as ListBoxItem;
|
226
|
+
|
227
|
+
lstFamilyName0.MaxHeight = (listBoxItem?.ActualHeight * 5 + 4) ?? 0; // 4=Border Padding BorderThickness分
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
{
|
232
|
+
|
233
|
+
var listBoxItem = lstFamilyName1.ItemContainerGenerator.ContainerFromItem(lstFamilyName1.ItemsSource.Cast<object>().FirstOrDefault()) as ListBoxItem;
|
234
|
+
|
235
|
+
lstFamilyName1.MaxHeight = (listBoxItem?.ActualHeight * 5 + 4) ?? 0; // 4=Border Padding BorderThickness分
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
var listBoxItem = lstFamilyName3.ItemContainerGenerator.ContainerFromItem(lstFamilyName3.ItemsSource.Cast<object>().FirstOrDefault()) as ListBoxItem;
|
242
|
+
|
243
|
+
lstFamilyName3.MaxHeight = (listBoxItem?.ActualHeight * 5 + 4) ?? 0; // 4=Border Padding BorderThickness分
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
{
|
248
|
+
|
249
|
+
var listBoxItem = lstFamilyName5.ItemContainerGenerator.ContainerFromItem(lstFamilyName5.ItemsSource.Cast<object>().FirstOrDefault()) as ListBoxItem;
|
250
|
+
|
251
|
+
lstFamilyName5.MaxHeight = (listBoxItem?.ActualHeight * 5 + 4) ?? 0; // 4=Border Padding BorderThickness分
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
{
|
256
|
+
|
257
|
+
var listBoxItem = lstFamilyName.ItemContainerGenerator.ContainerFromItem(lstFamilyName.ItemsSource.Cast<object>().FirstOrDefault()) as ListBoxItem;
|
258
|
+
|
259
|
+
lstFamilyName.MaxHeight = (listBoxItem?.ActualHeight * 5 + 4) ?? 0; // 4=Border Padding BorderThickness分
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
class SystemFontsModel
|
268
|
+
|
269
|
+
{
|
270
|
+
|
271
|
+
private static ICollection<FontFamily> _SystemFonts;
|
272
|
+
|
273
|
+
public static ICollection<FontFamily> SystemFonts => _SystemFonts ??= GetSystemFonts();
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
private static ICollection<FontFamily> GetSystemFonts()
|
278
|
+
|
279
|
+
{
|
280
|
+
|
281
|
+
var systemFonts = new List<FontFamily>();
|
282
|
+
|
283
|
+
var jp = XmlLanguage.GetLanguage("ja-jp");
|
284
|
+
|
285
|
+
var us = XmlLanguage.GetLanguage("en-us");
|
286
|
+
|
287
|
+
foreach(var font in Fonts.SystemFontFamilies)
|
288
|
+
|
289
|
+
{
|
290
|
+
|
291
|
+
if(font.FamilyNames.ContainsKey(jp)) systemFonts.Add(new FontFamily(font.FamilyNames[jp]));
|
292
|
+
|
293
|
+
else if(font.FamilyNames.ContainsKey(us)) systemFonts.Add(new FontFamily(font.FamilyNames[us]));
|
294
|
+
|
295
|
+
else systemFonts.Add(new FontFamily(font.FamilyNames.First().Value));
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
systemFonts.Sort((x, y) => string.Compare(x.Source, y.Source, StringComparison.Ordinal));
|
300
|
+
|
301
|
+
return systemFonts;
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
```
|
312
|
+
|
313
|
+
![アプリ画像](383a253147c4a02b7d63b9e94837986c.png)
|
314
|
+
|
315
|
+
多分ほかのリストボックスにも適用するんでしょうから、ビヘイビアのほうがいいでしょうね^^;
|
3
サイズ調整
test
CHANGED
@@ -14,15 +14,15 @@
|
|
14
14
|
|
15
15
|
Grid.RowSpan="5">
|
16
16
|
|
17
|
-
<TextBlock />
|
17
|
+
<TextBlock Margin="3" />
|
18
18
|
|
19
|
-
<TextBlock />
|
19
|
+
<TextBlock Margin="3" />
|
20
20
|
|
21
|
-
<TextBlock />
|
21
|
+
<TextBlock Margin="3" />
|
22
22
|
|
23
|
-
<TextBlock />
|
23
|
+
<TextBlock Margin="3" />
|
24
24
|
|
25
|
-
<TextBlock />
|
25
|
+
<TextBlock Margin="3" />
|
26
26
|
|
27
27
|
</ListBox>
|
28
28
|
|
@@ -52,7 +52,7 @@
|
|
52
52
|
|
53
53
|
var listBoxItem = lstFamilyName.ItemContainerGenerator.ContainerFromItem(lstFamilyName.ItemsSource.Cast<object>().First()) as ListBoxItem;
|
54
54
|
|
55
|
-
lstFamilyName.Height = listBoxItem.ActualHeight * 5;
|
55
|
+
lstFamilyName.Height = listBoxItem.ActualHeight * 5 + 4; // 4=BorderのPadding BorderThickness分
|
56
56
|
|
57
57
|
}
|
58
58
|
|
2
5
test
CHANGED
@@ -13,8 +13,6 @@
|
|
13
13
|
Grid.Row="2"
|
14
14
|
|
15
15
|
Grid.RowSpan="5">
|
16
|
-
|
17
|
-
<TextBlock />
|
18
16
|
|
19
17
|
<TextBlock />
|
20
18
|
|
1
5
test
CHANGED
@@ -13,8 +13,6 @@
|
|
13
13
|
Grid.Row="2"
|
14
14
|
|
15
15
|
Grid.RowSpan="5">
|
16
|
-
|
17
|
-
<TextBlock />
|
18
16
|
|
19
17
|
<TextBlock />
|
20
18
|
|
@@ -56,7 +54,7 @@
|
|
56
54
|
|
57
55
|
var listBoxItem = lstFamilyName.ItemContainerGenerator.ContainerFromItem(lstFamilyName.ItemsSource.Cast<object>().First()) as ListBoxItem;
|
58
56
|
|
59
|
-
lstFamilyName.Height = listBoxItem.ActualHeight *
|
57
|
+
lstFamilyName.Height = listBoxItem.ActualHeight * 5;
|
60
58
|
|
61
59
|
}
|
62
60
|
|