teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

見直しキャンペーン中

2023/07/29 06:18

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -95,4 +95,6 @@
95
95
 
96
96
  > 下記のコードにこだわりがあるわけではない為、寿司打を再現できる簡単な方法があればよいのですが…
97
97
 
98
- 雑にやるなら`TextBlock`を重ねるとか?(ちょっと文字が太ってみえる)
98
+ 雑にやるなら`TextBlock`を重ねるとか?(ちょっと文字が太ってみえる)
99
+
100
+ ![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-29/6f285c71-035c-4670-8118-db6ba680f867.gif)

3

見直しキャンペーン中

2023/07/29 06:07

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -1,98 +1,98 @@
1
- ぶっちゃけなにもわかってません^^;
2
-
3
- [TextPointer.GetPositionAtOffset メソッド (System.Windows.Documents) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.documents.textpointer.getpositionatoffset)
4
- は「シンボル単位」となっており、「文字単位」ではないようです。
5
- そのため`TextPointer`を、うまく送れてないのではないでしょうか?
6
-
7
- 適当にggって見つけたコードで置き換えたところ、それっぽく動いている気はします。
8
- [c# - Select Range of Text in WPF RichTextBox (FlowDocument) Programmatically - Stack Overflow](https://stackoverflow.com/questions/1454440/select-range-of-text-in-wpf-richtextbox-flowdocument-programmatically/12399115#12399115)
9
-
10
- ```xaml
11
- <Window
12
- x:Class="Questions361934.MainWindow"
13
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
15
- Width="800"
16
- Height="450">
17
- <Grid>
18
- <Grid.RowDefinitions>
19
- <RowDefinition />
20
- <RowDefinition />
21
- <RowDefinition />
22
- </Grid.RowDefinitions>
23
- <RichTextBox x:Name="rtb">
24
- <FlowDocument>
25
- <Paragraph>
26
- tonarinokyakuhayokukakikuukyakuda
27
- </Paragraph>
28
- </FlowDocument>
29
- </RichTextBox>
30
-
31
- <TextBlock Grid.Row="1" Text="tonarinokyakuhayokukakikuukyakuda" />
32
- <TextBlock
33
- x:Name="textBlock"
34
- Grid.Row="1"
35
- Foreground="Red" />
36
-
37
- <TextBox
38
- x:Name="tb"
39
- Grid.Row="2"
40
- TextChanged="TextChanged" />
41
- </Grid>
42
- </Window>
43
- ```
44
-
45
- ```C#
46
- using System.Windows;
47
- using System.Windows.Controls;
48
- using System.Windows.Documents;
49
- using System.Windows.Media;
50
-
51
- namespace Questions361934
52
- {
53
- public partial class MainWindow : Window
54
- {
55
- public MainWindow() => InitializeComponent();
56
-
57
- private void TextChanged(object sender, TextChangedEventArgs e)
58
- {
59
- var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
60
- textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
61
-
62
- textBlock.Text = "";
63
-
64
- var kaitou = "tonarinokyakuhayokukakikuukyakuda";
65
- var ans = tb.Text;
66
- if (kaitou.StartsWith(ans))
67
- {
68
- var p = GetPoint(rtb.Document.ContentStart, ans.Length);
69
- textRange = new TextRange(rtb.Document.ContentStart, p);
70
- textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
71
-
72
- textBlock.Text = ans;
73
- }
74
- }
75
-
76
- // [c# - Select Range of Text in WPF RichTextBox (FlowDocument) Programmatically - Stack Overflow](https://stackoverflow.com/questions/1454440/select-range-of-text-in-wpf-richtextbox-flowdocument-programmatically/12399115#12399115)
77
- private static TextPointer GetPoint(TextPointer start, int x)
78
- {
79
- var ret = start;
80
- var i = 0;
81
- while (ret != null)
82
- {
83
- var stringSoFar = new TextRange(ret, ret.GetPositionAtOffset(i, LogicalDirection.Forward)).Text;
84
- if (stringSoFar.Length == x) break;
85
-
86
- i++;
87
- if (ret.GetPositionAtOffset(i, LogicalDirection.Forward) == null)
88
- return ret.GetPositionAtOffset(i - 1, LogicalDirection.Forward);
89
- }
90
- return ret.GetPositionAtOffset(i, LogicalDirection.Forward);
91
- }
92
- }
93
- }
94
- ```
95
-
96
- > 下記のコードにこだわりがあるわけではない為、寿司打を再現できる簡単な方法があればよいのですが…
97
-
1
+ ぶっちゃけなにもわかってません^^;
2
+
3
+ [TextPointer.GetPositionAtOffset メソッド (System.Windows.Documents) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.documents.textpointer.getpositionatoffset)
4
+ は「シンボル単位」となっており、「文字単位」ではないようです。
5
+ そのため`TextPointer`を、うまく送れてないのではないでしょうか?
6
+
7
+ 適当にggって見つけたコードで置き換えたところ、それっぽく動いている気はします。
8
+ [c# - Select Range of Text in WPF RichTextBox (FlowDocument) Programmatically - Stack Overflow](https://stackoverflow.com/questions/1454440/select-range-of-text-in-wpf-richtextbox-flowdocument-programmatically/12399115#12399115)
9
+
10
+ ```xml
11
+ <Window
12
+ x:Class="Questions361934.MainWindow"
13
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
15
+ Width="800"
16
+ Height="450">
17
+ <Grid>
18
+ <Grid.RowDefinitions>
19
+ <RowDefinition />
20
+ <RowDefinition />
21
+ <RowDefinition />
22
+ </Grid.RowDefinitions>
23
+ <RichTextBox x:Name="rtb">
24
+ <FlowDocument>
25
+ <Paragraph>
26
+ tonarinokyakuhayokukakikuukyakuda
27
+ </Paragraph>
28
+ </FlowDocument>
29
+ </RichTextBox>
30
+
31
+ <TextBlock Grid.Row="1" Text="tonarinokyakuhayokukakikuukyakuda" />
32
+ <TextBlock
33
+ x:Name="textBlock"
34
+ Grid.Row="1"
35
+ Foreground="Red" />
36
+
37
+ <TextBox
38
+ x:Name="tb"
39
+ Grid.Row="2"
40
+ TextChanged="TextChanged" />
41
+ </Grid>
42
+ </Window>
43
+ ```
44
+
45
+ ```cs
46
+ using System.Windows;
47
+ using System.Windows.Controls;
48
+ using System.Windows.Documents;
49
+ using System.Windows.Media;
50
+
51
+ namespace Questions361934
52
+ {
53
+ public partial class MainWindow : Window
54
+ {
55
+ public MainWindow() => InitializeComponent();
56
+
57
+ private void TextChanged(object sender, TextChangedEventArgs e)
58
+ {
59
+ var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
60
+ textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
61
+
62
+ textBlock.Text = "";
63
+
64
+ var kaitou = "tonarinokyakuhayokukakikuukyakuda";
65
+ var ans = tb.Text;
66
+ if (kaitou.StartsWith(ans))
67
+ {
68
+ var p = GetPoint(rtb.Document.ContentStart, ans.Length);
69
+ textRange = new TextRange(rtb.Document.ContentStart, p);
70
+ textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
71
+
72
+ textBlock.Text = ans;
73
+ }
74
+ }
75
+
76
+ // [c# - Select Range of Text in WPF RichTextBox (FlowDocument) Programmatically - Stack Overflow](https://stackoverflow.com/questions/1454440/select-range-of-text-in-wpf-richtextbox-flowdocument-programmatically/12399115#12399115)
77
+ private static TextPointer GetPoint(TextPointer start, int x)
78
+ {
79
+ var ret = start;
80
+ var i = 0;
81
+ while (ret != null)
82
+ {
83
+ var stringSoFar = new TextRange(ret, ret.GetPositionAtOffset(i, LogicalDirection.Forward)).Text;
84
+ if (stringSoFar.Length == x) break;
85
+
86
+ i++;
87
+ if (ret.GetPositionAtOffset(i, LogicalDirection.Forward) == null)
88
+ return ret.GetPositionAtOffset(i - 1, LogicalDirection.Forward);
89
+ }
90
+ return ret.GetPositionAtOffset(i, LogicalDirection.Forward);
91
+ }
92
+ }
93
+ }
94
+ ```
95
+
96
+ > 下記のコードにこだわりがあるわけではない為、寿司打を再現できる簡単な方法があればよいのですが…
97
+
98
98
  雑にやるなら`TextBlock`を重ねるとか?(ちょっと文字が太ってみえる)

2

簡単な方法

2021/09/29 21:18

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -18,6 +18,7 @@
18
18
  <Grid.RowDefinitions>
19
19
  <RowDefinition />
20
20
  <RowDefinition />
21
+ <RowDefinition />
21
22
  </Grid.RowDefinitions>
22
23
  <RichTextBox x:Name="rtb">
23
24
  <FlowDocument>
@@ -26,9 +27,16 @@
26
27
  </Paragraph>
27
28
  </FlowDocument>
28
29
  </RichTextBox>
30
+
31
+ <TextBlock Grid.Row="1" Text="tonarinokyakuhayokukakikuukyakuda" />
32
+ <TextBlock
33
+ x:Name="textBlock"
34
+ Grid.Row="1"
35
+ Foreground="Red" />
36
+
29
37
  <TextBox
30
38
  x:Name="tb"
31
- Grid.Row="1"
39
+ Grid.Row="2"
32
40
  TextChanged="TextChanged" />
33
41
  </Grid>
34
42
  </Window>
@@ -50,6 +58,8 @@
50
58
  {
51
59
  var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
52
60
  textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
61
+
62
+ textBlock.Text = "";
53
63
 
54
64
  var kaitou = "tonarinokyakuhayokukakikuukyakuda";
55
65
  var ans = tb.Text;
@@ -58,6 +68,8 @@
58
68
  var p = GetPoint(rtb.Document.ContentStart, ans.Length);
59
69
  textRange = new TextRange(rtb.Document.ContentStart, p);
60
70
  textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
71
+
72
+ textBlock.Text = ans;
61
73
  }
62
74
  }
63
75
 
@@ -79,5 +91,8 @@
79
91
  }
80
92
  }
81
93
  }
94
+ ```
82
95
 
96
+ > 下記のコードにこだわりがあるわけではない為、寿司打を再現できる簡単な方法があればよいのですが…
97
+
83
- ```
98
+ 雑にやるなら`TextBlock`を重ねるとか?(ちょっと文字が太ってみえる)

1

()

2021/09/29 21:18

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  ぶっちゃけなにもわかってません^^;
2
2
 
3
3
  [TextPointer.GetPositionAtOffset メソッド (System.Windows.Documents) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.documents.textpointer.getpositionatoffset)
4
- は「(シンボル単位)」となっており、「文字単位」ではないようです。
4
+ は「シンボル単位」となっており、「文字単位」ではないようです。
5
5
  そのため`TextPointer`を、うまく送れてないのではないでしょうか?
6
6
 
7
7
  適当にggって見つけたコードで置き換えたところ、それっぽく動いている気はします。