回答編集履歴
3
微調整
answer
CHANGED
@@ -56,7 +56,7 @@
|
|
56
56
|
public class JobItem
|
57
57
|
{
|
58
58
|
public string JobCode { get; set; } = "";
|
59
|
-
public string JobName { get; set; }
|
59
|
+
public string JobName { get; set; } = "";
|
60
60
|
public string JobNameDisplay { get { return JobCode + ":" + JobName; } }
|
61
61
|
}
|
62
62
|
}
|
2
質問の内容が変わったため
answer
CHANGED
@@ -1,7 +1,67 @@
|
|
1
|
-
バインディングを使う必要が無ければXAMLのItemsSourceとDisplayMemberPathを削除し、
|
2
|
-
|
1
|
+
例えばこうです。
|
3
2
|
|
4
|
-
|
3
|
+
XAMLからBindingを消しておきます。
|
5
4
|
|
5
|
+
```xaml
|
6
|
+
<Window x:Class="WpfApplication18.MainWindow"
|
7
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
8
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
9
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
10
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
11
|
+
xmlns:local="clr-namespace:WpfApplication18"
|
12
|
+
mc:Ignorable="d"
|
13
|
+
Title="MainWindow" Height="350" Width="525">
|
14
|
+
<Canvas>
|
15
|
+
<ComboBox Name="Cb" DisplayMemberPath="JobNameDisplay" Width="176" Canvas.Left="18" Canvas.Top="42"/>
|
16
|
+
</Canvas>
|
17
|
+
</Window>
|
18
|
+
```
|
19
|
+
|
20
|
+
InitializeComponent()の後辺りにファイルからの読み込み処理が入っています。
|
21
|
+
|
22
|
+
```csharp
|
23
|
+
using System.Collections.Generic;
|
6
|
-
|
24
|
+
using System.Linq;
|
25
|
+
using System.Text;
|
26
|
+
using System.Windows;
|
27
|
+
|
28
|
+
namespace WpfApplication18
|
29
|
+
{
|
30
|
+
/// <summary>
|
31
|
+
/// MainWindow.xaml の相互作用ロジック
|
32
|
+
/// </summary>
|
33
|
+
public partial class MainWindow : Window
|
34
|
+
{
|
35
|
+
private string _defaultJobCode = "b000";
|
36
|
+
private List<JobItem> _jobItems = new List<JobItem>();
|
37
|
+
|
38
|
+
public MainWindow()
|
39
|
+
{
|
40
|
+
InitializeComponent();
|
41
|
+
|
42
|
+
var lines = System.IO.File.ReadAllLines(@"C:\JobList.txt", Encoding.GetEncoding("shift_jis"));
|
43
|
+
var jobItems = lines.Select(line =>
|
44
|
+
{
|
45
|
+
var cols = line.Split(',');
|
46
|
+
return new JobItem() { JobCode = cols[0], JobName = cols[1] };
|
47
|
+
});
|
48
|
+
_jobItems.AddRange(jobItems);
|
49
|
+
|
50
|
+
Cb.ItemsSource = _jobItems;
|
51
|
+
Cb.SelectedItem = _jobItems.FirstOrDefault(item => item.JobCode == _defaultJobCode);
|
52
|
+
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
public class JobItem
|
57
|
+
{
|
58
|
+
public string JobCode { get; set; } = "";
|
59
|
+
public string JobName { get; set; }
|
60
|
+
public string JobNameDisplay { get { return JobCode + ":" + JobName; } }
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
```
|
65
|
+
|
66
|
+
カンマで区切られたものは、Splitでいい加減に分断していますが、
|
7
|
-
|
67
|
+
例えば役職名にカンマが入っていたらこのコードでは正しく動かない点に注意してください。
|
1
修正
answer
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
バインディングを使う必要が無ければXAMLのItemsSourceとDisplayMemberPathを削除し、
|
2
|
-
|
2
|
+
InitializeComponentの次辺りに
|
3
3
|
|
4
4
|
> Cb.ItemsSource = System.IO.File.ReadAllLines(@"C:\JobList.txt", Encoding.GetEncoding("shift_jis"));
|
5
5
|
|
6
|
-
|
6
|
+
とでも書けばコンボに反映されます。
|
7
7
|
ファイルパスとエンコーディングは適当に変えてください。
|