C#,WPF,ソフトウェア開発

Windows7のVirtualStoreが登場する前は、
アプリケーションデータをEXEと同ディレクトリに設置しすることで、
アプリケーションを削除したい際、ディレクトリごと削除するだけで
環境を汚さずに処理することができた。

しかしVirtualStore実装以降、ProgramFilesディレクトリにアプリケーションを設置した場合、
最新のデータはAppDataに格納されてしまい、ProgramFilesに保存されたデータを手で変更したとしても、
AppDataのデータで上書きされてしまう問題が発生する。

アプリケーションを、 ProgramFilesに設置しようが、他のディレクトリに設置しようが、
AppDataにデータを格納してしまえば良いという考え方もあるが、
なるべくであれば、ProgramFiles以外の場合はEXEと同じディレクトリに設置したいと考え、
下記のようなコードでディレクトリパス取得を行った。

C#,WPF,ソフトウェア開発

ぐらばく氏の「WPF でウィンドウ位置とサイズを保存・復元しよう」を元に
ApplicationSettingsBaseのGradeUp対応を含んだカスタマイズを行った。

C#,WPF,ソフトウェア開発

BindingにStringFormatを組み合わせることで、元データでデータを編集することなく、
桁区切りや日付のフォーマットなどを調整することができる。

また、ElementStyleにてTextAlignmentを設定することで、右寄せ・中央寄せができる。

C#,WPF,ソフトウェア開発

1次元配列であれば、DataGridにBindするだけで表示されるが、
クラスや構造体を用いた多次元配列をDataGridにBindする場合、
{Binding [プロパティ名]} が {Binding Path=[プロパティ名].[プロパティ名]} に変わる。

CSファイル

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // 生徒配列
        List<Student> student = new List<Student>();

        student.Add(new Student(1, "塩沢 美智子", new Test(50, 60, 40, 50)));
        student.Add(new Student(2, "志村 薫", new Test(75, 56, 97, 30)));
        student.Add(new Student(3, "石崎 なつみ", new Test(75, 21, 26, 34)));
        student.Add(new Student(3, "島袋 ジョージ", new Test(77, 96, 64, 37)));
        student.Add(new Student(3, "森田 サンタマリア", new Test(99, 79, 61, 74)));

        grdData.DataContext = student;
    }
}

/// <summary>
/// 生徒クラス
/// </summary>
public class Student
{
    // 出席番号
    public int no { get; }

    // 名前
    public string name { get; }

    // テスト結果
    public Test test { get; }

    public Student(int _no, string _name, Test _test)
    {
        this.no = _no;
        this.name = _name;
        this.test = _test;
    }
}

/// <summary>
/// テスト結果クラス
/// </summary>
public class Test
{
    // 国語
    public int kokugo { get; }

    // 算数
    public int sansu { get; }

    // 理科
    public int rika { get; }

    // 社会
    public int syakai { get; }

    public Test(int _kokugo, int _sansu, int _rika, int _syakai)
    {
        this.kokugo = _kokugo;
        this.sansu = _sansu;
        this.rika = _rika;
        this.syakai = _syakai;
    }
}

XAMLファイル

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="grdData" Margin="10" ItemsSource="{Binding}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="出席番号" Binding="{Binding no}" />
                <DataGridTextColumn Header="名前" Binding="{Binding name}" />
                <DataGridTextColumn Header="国語" Binding="{Binding Path=test.kokugo}" />
                <DataGridTextColumn Header="算数" Binding="{Binding Path=test.sansu}" />
                <DataGridTextColumn Header="理科" Binding="{Binding Path=test.rika}" />
                <DataGridTextColumn Header="社会" Binding="{Binding Path=test.syakai}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

C#,WPF,ソフトウェア開発,メモ

マテリアルデザインのウィンドウにしてくれるライブラリ

Material Design In XAML Toolkit

 

ウィンドウの枠を光らせてくれるライブラリ
MetroRadiance

C#,WPF,ソフトウェア開発

App.xamlのStartupUriを消去し、自動起動される画面を除去

・修正前

<Application x:Class="WpfApplication.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

・修正後

<Application x:Class="WpfApplication.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication">
    <Application.Resources>
         
    </Application.Resources>
</Application>

 

App.xaml.csにOnStartupをオーバーライドし、各画面オブジェクトを生成、Showする。

    public partial class App : Application
    {

        static MainWindow objMainWindow;
        static MainWindow objMainWindow2;

        protected override void OnStartup(StartupEventArgs e)
        {
            objMainWindow = new MainWindow();
            objMainWindow2 = new MainWindow();

            objMainWindow.Show();
            objMainWindow2.Show();
        }

    }

 

全ての画面が閉じられると、プロセスが終了する。