Last active
August 29, 2015 14:01
-
-
Save pierre3/177a4a84fa058e461414 to your computer and use it in GitHub Desktop.
[WPF]CSVファイルを読み込んでDataGridに表示する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WpfApp.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:app="clr-namespace:WpfApp" | |
Title="MainWindow" Height="640" Width="800"> | |
<Window.DataContext> | |
<app:CsvData/> | |
</Window.DataContext> | |
<Grid> | |
<Grid.RowDefinitions> | |
<RowDefinition/> | |
<RowDefinition Height="Auto"/> | |
</Grid.RowDefinitions> | |
<DataGrid Grid.Row="0" | |
HorizontalAlignment="Stretch" | |
Margin="4" | |
VerticalAlignment="Stretch" | |
ItemsSource="{Binding Rows}" | |
AutoGenerateColumns="True" /> | |
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center"> | |
<Button Margin="4" Padding="8,4" Click="ReadButtonClick">Read</Button> | |
<Button Margin="4" Padding="8,4" Click="CancelButtonClick">Cancel</Button> | |
</StackPanel> | |
</Grid> | |
</Window> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using HandyUtil.Extensions.System; | |
using HandyUtil.Text.Xsv; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reactive.Linq; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Collections.ObjectModel; | |
namespace WpfApp | |
{ | |
/// <summary> | |
/// MainWindow.xaml の相互作用ロジック | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
private CsvData VM { get { return DataContext as CsvData; } } | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
private void ReadButtonClick(object sender, RoutedEventArgs e) | |
{ | |
if (VM == null) { return; } | |
VM.Clear(); | |
VM.Read(); | |
} | |
private void CancelButtonClick(object sender, RoutedEventArgs e) | |
{ | |
if (VM == null) { return; } | |
VM.Cancel(); | |
} | |
} | |
/// <summary> | |
/// 1レコード分のデータ | |
/// </summary> | |
public class ModelShip | |
{ | |
public int No { set; get; } | |
public string ClassOfShip { set; get; } | |
public string Name { set; get; } | |
public int TaxIncludedPrice { set; get; } | |
public int Price { set; get; } | |
public string Maker { set; get; } | |
public static ModelShip FromCsvRecord(IList<string> header, IList<string> row) | |
{ | |
var keyedRow = header.Zip(row, (h, r) => new { h, r }).ToDictionary(a => a.h, a => a.r); | |
return new ModelShip() | |
{ | |
No = keyedRow["No."].ToInt32(), | |
ClassOfShip = keyedRow["艦種"], | |
Name = keyedRow["品名"], | |
TaxIncludedPrice = keyedRow["税込価格"].ToInt32OrDefault(System.Globalization.NumberStyles.Currency, -1), | |
Price = keyedRow["本体価格"].ToInt32OrDefault(System.Globalization.NumberStyles.Currency, -1), | |
Maker = keyedRow["メーカー"] | |
}; | |
} | |
} | |
/// <summary> | |
/// CSVデータを管理する(ViewModel) | |
/// </summary> | |
public class CsvData | |
{ | |
private IDisposable disposable = System.Reactive.Disposables.Disposable.Empty; | |
public ObservableCollection<ModelShip> Rows { set; get; } | |
public CsvData() | |
{ | |
Rows = new ObservableCollection<ModelShip>(); | |
} | |
public void Clear() | |
{ | |
Rows.Clear(); | |
} | |
public void Cancel() | |
{ | |
System.Diagnostics.Debug.WriteLine("cancel"); | |
if (disposable != null) | |
{ | |
disposable.Dispose(); | |
disposable = null; | |
} | |
} | |
public void Read() | |
{ | |
Cancel(); | |
var reader = new XsvReader(new System.IO.StreamReader(@".\ModelShips.txt")); | |
var header = reader.ReadXsvLine(new[] { "," }).ToList(); | |
disposable = reader.ReadXsvAsObservable(new[] { "," }) | |
.ObserveOn(System.Threading.SynchronizationContext.Current) | |
.Subscribe( | |
row => | |
{ | |
System.Diagnostics.Debug.WriteLine("OnNext"); | |
this.Rows.Add(ModelShip.FromCsvRecord(header,row)); | |
}, | |
error => | |
{ | |
System.Diagnostics.Debug.WriteLine("OnError:" + error); | |
reader.Dispose(); | |
}, | |
() => | |
{ | |
System.Diagnostics.Debug.WriteLine("OnCompleted"); | |
reader.Dispose(); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment