回答編集履歴
2
見直しキャンペーン中
answer
CHANGED
@@ -123,6 +123,7 @@
|
|
123
123
|
}
|
124
124
|
}
|
125
125
|
```
|
126
|
+

|
126
127
|
|
127
128
|
---
|
128
129
|
|
1
見直しキャンペーン中
answer
CHANGED
@@ -1,128 +1,129 @@
|
|
1
|
-
> カーソルを合わせても何も起こらないListBoxの作成方法
|
2
|
-
|
3
|
-
`ItemsControl`がそれです。
|
4
|
-
|
5
|
-
> 1要素の追加と削除に対しての効率的な文字列への変換方法
|
6
|
-
|
7
|
-
動かしてみた感じ、コード部分のせいではなく`TextBlock`の反映が遅いような気がします(ボタンの連打はできるが、表示がついてこない)
|
8
|
-
行数の多い`TextBlock`自体が重いとなると、やはり仮想化を有効にした`ItemsControl`が最適です。
|
9
|
-
|
10
|
-
`TextBlock.Inlines`に`Run`を追加削除もやってみましたが、お話にならないぐらい遅かったです。
|
11
|
-
|
12
|
-
```
|
13
|
-
<Window
|
14
|
-
x:Class="Questions291838.MainWindow"
|
15
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
16
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
17
|
-
Width="800"
|
18
|
-
Height="450">
|
19
|
-
<Grid>
|
20
|
-
<Grid.ColumnDefinitions>
|
21
|
-
<ColumnDefinition />
|
22
|
-
<ColumnDefinition />
|
23
|
-
</Grid.ColumnDefinitions>
|
24
|
-
|
25
|
-
<GroupBox Header="TextBlock">
|
26
|
-
<DockPanel>
|
27
|
-
<Button
|
28
|
-
Click="TextBlockButton_Click"
|
29
|
-
Content="Add"
|
30
|
-
DockPanel.Dock="Top" />
|
31
|
-
<ScrollViewer VerticalScrollBarVisibility="Auto">
|
32
|
-
<TextBlock x:Name="tb" />
|
33
|
-
</ScrollViewer>
|
34
|
-
</DockPanel>
|
35
|
-
</GroupBox>
|
36
|
-
|
37
|
-
<GroupBox Grid.Column="1" Header="ItemsControl">
|
38
|
-
<DockPanel>
|
39
|
-
<Button
|
40
|
-
Click="ItemsControlButton_Click"
|
41
|
-
Content="Add"
|
42
|
-
DockPanel.Dock="Top" />
|
43
|
-
<ItemsControl x:Name="ic">
|
44
|
-
<ItemsControl.ItemsPanel>
|
45
|
-
<ItemsPanelTemplate>
|
46
|
-
<VirtualizingStackPanel />
|
47
|
-
</ItemsPanelTemplate>
|
48
|
-
</ItemsControl.ItemsPanel>
|
49
|
-
<ItemsControl.Template>
|
50
|
-
<ControlTemplate>
|
51
|
-
<ScrollViewer VerticalScrollBarVisibility="Auto">
|
52
|
-
<ItemsPresenter />
|
53
|
-
</ScrollViewer>
|
54
|
-
</ControlTemplate>
|
55
|
-
</ItemsControl.Template>
|
56
|
-
</ItemsControl>
|
57
|
-
</DockPanel>
|
58
|
-
</GroupBox>
|
59
|
-
</Grid>
|
60
|
-
</Window>
|
61
|
-
```
|
62
|
-
|
63
|
-
|
64
|
-
using System
|
65
|
-
using System.Collections.
|
66
|
-
using System.
|
67
|
-
using System.
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
private const
|
75
|
-
|
76
|
-
|
77
|
-
private readonly
|
78
|
-
private readonly
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
//
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
1
|
+
> カーソルを合わせても何も起こらないListBoxの作成方法
|
2
|
+
|
3
|
+
`ItemsControl`がそれです。
|
4
|
+
|
5
|
+
> 1要素の追加と削除に対しての効率的な文字列への変換方法
|
6
|
+
|
7
|
+
動かしてみた感じ、コード部分のせいではなく`TextBlock`の反映が遅いような気がします(ボタンの連打はできるが、表示がついてこない)
|
8
|
+
行数の多い`TextBlock`自体が重いとなると、やはり仮想化を有効にした`ItemsControl`が最適です。
|
9
|
+
|
10
|
+
`TextBlock.Inlines`に`Run`を追加削除もやってみましたが、お話にならないぐらい遅かったです。
|
11
|
+
|
12
|
+
```xml
|
13
|
+
<Window
|
14
|
+
x:Class="Questions291838.MainWindow"
|
15
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
16
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
17
|
+
Width="800"
|
18
|
+
Height="450">
|
19
|
+
<Grid>
|
20
|
+
<Grid.ColumnDefinitions>
|
21
|
+
<ColumnDefinition />
|
22
|
+
<ColumnDefinition />
|
23
|
+
</Grid.ColumnDefinitions>
|
24
|
+
|
25
|
+
<GroupBox Header="TextBlock">
|
26
|
+
<DockPanel>
|
27
|
+
<Button
|
28
|
+
Click="TextBlockButton_Click"
|
29
|
+
Content="Add"
|
30
|
+
DockPanel.Dock="Top" />
|
31
|
+
<ScrollViewer VerticalScrollBarVisibility="Auto">
|
32
|
+
<TextBlock x:Name="tb" />
|
33
|
+
</ScrollViewer>
|
34
|
+
</DockPanel>
|
35
|
+
</GroupBox>
|
36
|
+
|
37
|
+
<GroupBox Grid.Column="1" Header="ItemsControl">
|
38
|
+
<DockPanel>
|
39
|
+
<Button
|
40
|
+
Click="ItemsControlButton_Click"
|
41
|
+
Content="Add"
|
42
|
+
DockPanel.Dock="Top" />
|
43
|
+
<ItemsControl x:Name="ic">
|
44
|
+
<ItemsControl.ItemsPanel>
|
45
|
+
<ItemsPanelTemplate>
|
46
|
+
<VirtualizingStackPanel />
|
47
|
+
</ItemsPanelTemplate>
|
48
|
+
</ItemsControl.ItemsPanel>
|
49
|
+
<ItemsControl.Template>
|
50
|
+
<ControlTemplate>
|
51
|
+
<ScrollViewer VerticalScrollBarVisibility="Auto">
|
52
|
+
<ItemsPresenter />
|
53
|
+
</ScrollViewer>
|
54
|
+
</ControlTemplate>
|
55
|
+
</ItemsControl.Template>
|
56
|
+
</ItemsControl>
|
57
|
+
</DockPanel>
|
58
|
+
</GroupBox>
|
59
|
+
</Grid>
|
60
|
+
</Window>
|
61
|
+
```
|
62
|
+
|
63
|
+
```cs
|
64
|
+
using System;
|
65
|
+
using System.Collections.Generic;
|
66
|
+
using System.Collections.ObjectModel;
|
67
|
+
using System.Linq;
|
68
|
+
using System.Windows;
|
69
|
+
|
70
|
+
namespace Questions291838
|
71
|
+
{
|
72
|
+
public partial class MainWindow : Window
|
73
|
+
{
|
74
|
+
private const int maxCount = 2000;
|
75
|
+
private const string letters = "abcdefghijklmnopqrstuvwxyz";
|
76
|
+
|
77
|
+
private readonly List<string> tbLogs = new List<string>();
|
78
|
+
private readonly ObservableCollection<string> icLogs = new ObservableCollection<string>();
|
79
|
+
private readonly Random r = new Random();
|
80
|
+
|
81
|
+
public MainWindow()
|
82
|
+
{
|
83
|
+
InitializeComponent();
|
84
|
+
|
85
|
+
for(var i = 0; i < maxCount; i++)
|
86
|
+
{
|
87
|
+
tbLogs.Add(RandomWord());
|
88
|
+
icLogs.Add(RandomWord());
|
89
|
+
}
|
90
|
+
tb.Text = string.Join("\n", tbLogs);
|
91
|
+
ic.ItemsSource = icLogs;
|
92
|
+
}
|
93
|
+
|
94
|
+
// TextBlock側をできるだけ有利にしたつもり
|
95
|
+
// 「少しは速くなるかな?」と思い書き換えた部分があるが、特にベンチマークを取ったわけではない
|
96
|
+
private void TextBlockButton_Click(object sender, RoutedEventArgs e)
|
97
|
+
{
|
98
|
+
for(var i = 0; i < 10; i++)
|
99
|
+
{
|
100
|
+
tbLogs.Insert(0, RandomWord());
|
101
|
+
}
|
102
|
+
if(tbLogs.Count > maxCount)
|
103
|
+
{
|
104
|
+
tbLogs.RemoveRange(maxCount, tbLogs.Count - maxCount);
|
105
|
+
}
|
106
|
+
tb.Text = string.Join("\n", tbLogs);
|
107
|
+
}
|
108
|
+
|
109
|
+
private void ItemsControlButton_Click(object sender, RoutedEventArgs e)
|
110
|
+
{
|
111
|
+
for(var i = 0; i < 10; i++)
|
112
|
+
{
|
113
|
+
icLogs.Insert(0, RandomWord());
|
114
|
+
while(icLogs.Count > maxCount)
|
115
|
+
{
|
116
|
+
icLogs.RemoveAt(icLogs.Count - 1);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
// 1行にしたかっただけ 速さはしらない^^;
|
122
|
+
private string RandomWord() => string.Join("", Enumerable.Range(0, r.Next(1, 20)).Select(x => letters[r.Next(0, letters.Length - 1)]));
|
123
|
+
}
|
124
|
+
}
|
125
|
+
```
|
126
|
+
|
127
|
+
---
|
128
|
+
|
128
129
|
さらに改良の余地もありそうですが、体感では差がわからなそうなのでやめました^^;
|