回答編集履歴

3

微調整

2018/08/01 08:49

投稿

takabosoft
takabosoft

スコア8356

test CHANGED
@@ -114,7 +114,7 @@
114
114
 
115
115
  public string JobCode { get; set; } = "";
116
116
 
117
- public string JobName { get; set; }
117
+ public string JobName { get; set; } = "";
118
118
 
119
119
  public string JobNameDisplay { get { return JobCode + ":" + JobName; } }
120
120
 

2

質問の内容が変わったため

2018/08/01 08:49

投稿

takabosoft
takabosoft

スコア8356

test CHANGED
@@ -1,13 +1,133 @@
1
- バインディングを使う必要が無ければXAMLのItemsSourceとDisplayMemberPathを削除し、
2
-
3
- InitializeComponentの次辺りに
1
+ 例えばこうです。
4
2
 
5
3
 
6
4
 
7
- > Cb.ItemsSource = System.IO.File.ReadAllLines(@"C:\JobList.txt", Encoding.GetEncoding("shift_jis"));
5
+ XAMLからBindingを消しておきます。
8
6
 
9
7
 
10
8
 
11
- とでも書けばコンボに反映されます。
9
+ ```xaml
12
10
 
11
+ <Window x:Class="WpfApplication18.MainWindow"
12
+
13
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
+
15
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
16
+
17
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
18
+
19
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
20
+
21
+ xmlns:local="clr-namespace:WpfApplication18"
22
+
23
+ mc:Ignorable="d"
24
+
25
+ Title="MainWindow" Height="350" Width="525">
26
+
27
+ <Canvas>
28
+
29
+ <ComboBox Name="Cb" DisplayMemberPath="JobNameDisplay" Width="176" Canvas.Left="18" Canvas.Top="42"/>
30
+
31
+ </Canvas>
32
+
33
+ </Window>
34
+
35
+ ```
36
+
37
+
38
+
39
+ InitializeComponent()の後辺りにファイルからの読み込み処理が入っています。
40
+
41
+
42
+
43
+ ```csharp
44
+
45
+ using System.Collections.Generic;
46
+
47
+ using System.Linq;
48
+
49
+ using System.Text;
50
+
51
+ using System.Windows;
52
+
53
+
54
+
55
+ namespace WpfApplication18
56
+
57
+ {
58
+
59
+ /// <summary>
60
+
61
+ /// MainWindow.xaml の相互作用ロジック
62
+
63
+ /// </summary>
64
+
65
+ public partial class MainWindow : Window
66
+
67
+ {
68
+
69
+ private string _defaultJobCode = "b000";
70
+
71
+ private List<JobItem> _jobItems = new List<JobItem>();
72
+
73
+
74
+
75
+ public MainWindow()
76
+
77
+ {
78
+
79
+ InitializeComponent();
80
+
81
+
82
+
83
+ var lines = System.IO.File.ReadAllLines(@"C:\JobList.txt", Encoding.GetEncoding("shift_jis"));
84
+
85
+ var jobItems = lines.Select(line =>
86
+
87
+ {
88
+
89
+ var cols = line.Split(',');
90
+
91
+ return new JobItem() { JobCode = cols[0], JobName = cols[1] };
92
+
93
+ });
94
+
95
+ _jobItems.AddRange(jobItems);
96
+
97
+
98
+
99
+ Cb.ItemsSource = _jobItems;
100
+
101
+ Cb.SelectedItem = _jobItems.FirstOrDefault(item => item.JobCode == _defaultJobCode);
102
+
103
+
104
+
105
+ }
106
+
107
+ }
108
+
109
+
110
+
111
+ public class JobItem
112
+
113
+ {
114
+
115
+ public string JobCode { get; set; } = "";
116
+
117
+ public string JobName { get; set; }
118
+
119
+ public string JobNameDisplay { get { return JobCode + ":" + JobName; } }
120
+
121
+ }
122
+
123
+ }
124
+
125
+
126
+
127
+ ```
128
+
129
+
130
+
131
+ カンマで区切られたものは、Splitでいい加減に分断していますが、
132
+
13
- ファイルパスとエンコーディング適当変えてください。
133
+ 例えば役職名にカマが入っていたらこのコードで正しく動かない点注意してください。

1

修正

2018/08/01 08:47

投稿

takabosoft
takabosoft

スコア8356

test CHANGED
@@ -1,6 +1,6 @@
1
1
  バインディングを使う必要が無ければXAMLのItemsSourceとDisplayMemberPathを削除し、
2
2
 
3
- どこかで
3
+ InitializeComponentの次辺りに
4
4
 
5
5
 
6
6
 
@@ -8,6 +8,6 @@
8
8
 
9
9
 
10
10
 
11
- を実行すればコンボに反映されます。
11
+ とでも書けばコンボに反映されます。
12
12
 
13
13
  ファイルパスとエンコーディングは適当に変えてください。