はじめてのC#アプリケーション


この記事のなりわし

とある件で,先輩の作ったWindowsアプリケーションの改修をすることとなり,
全く経験のないなかで,いろいろまとめていった忘却禄

UWPとWPF

そもそも2種類あるのすら知らなかった.
UWPのほうが新しくて速いらしい
WPFはhtml,css,js みたいにかけて、簡単だけど、動作が遅い
アーキテクチャが全くの別物らしい
ちなみに,UWPはWindows10でしか動かない.

WPFのきほん

画面は.xamlファイル,処理は.xaml.csで書くらしい
https://qiita.com/koara-local/items/c58ca6b83e46b7c6125c

UI Debug

Live Visual Tree を使えばよいらしい
https://www.atmarkit.co.jp/ait/articles/1508/05/news023.html

用語集

【 C# 】 初心者による初心者のためのプログラミング用語の解説

ResourceDictionary

html5でいうところの,css的なことができる
https://www.doraxdora.com/blog/2017/06/08/post-1155/
https://docs.microsoft.com/ja-jp/windows/uwp/design/controls-and-patterns/resourcedictionary-and-xaml-resource-references
https://araramistudio.jimdo.com/2016/12/01/wpf%E3%81%A7%E3%82%88%E3%81%8F%E4%BD%BF%E3%81%86xaml%E3%81%AE%E5%AE%9A%E7%BE%A9%E3%82%92%E5%88%A5%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AB%E3%81%99%E3%82%8B/

Style/*.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="Body_Content_DataGrid_Centering" TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

使い方
StaticResourceで呼び出す

*.xaml
<DataGrid CellStyle="{StaticResource Body_Content_DataGrid_Centering}">
</DataGrid>

Data grid

ソーティング機能付きの表が作れる

xaml
<Window x:Class="DataGridBindingSample.MainWindow"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        Title="MainWindow" Height="200" Width="360">  
    <Grid>  
        <DataGrid Name="dataGrid" IsReadOnly="True"  
                  AutoGenerateColumns="False" >  
            <DataGrid.Columns>  
                <DataGridTextColumn Header="番号" Width="80"  
                                    Binding="{Binding No, StringFormat=D2}" />  
                <DataGridTextColumn Header="名前" Width="100"  
                                    Binding="{Binding Name}" />  
                <DataGridTextColumn Header="誕生日" Width="*"  
                                    Binding="{Binding BirthDay, StringFormat=yyyy/MM/dd}" />  
            </DataGrid.Columns>  
        </DataGrid>  
    </Grid>  
</Window>  
xaml.cs
public partial class MainWindow : Window  
{  
    public MainWindow()  
    {  
        InitializeComponent();  

        this.dataGrid.ItemsSource = new[]  
        {  
            new Person { No = 1, Name = "Tanaka", BirthDay = new DateTime(2000, 1, 1) },  
            new Person { No = 2, Name = "Yamada", BirthDay = new DateTime(1990, 5, 5) },  
            new Person { No = 3, Name = "Sato", BirthDay = new DateTime(2001, 12, 31) },  
        };  
    }  
}  
public class Person  
{  
    public int No { get; set; }  
    public string Name { get; set; }  
    public DateTime BirthDay { get; set; }  
}  

参考文献

WPF DataGridへのBindingに関する基本設計