C#,WPF

コンボボックスにMaterial Design In XAML Toolkitを適用すると、
現在選択されている項目が、ドロップダウンの一覧に表示されない形式となる。

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

初期のMainWindow.xaml

<Controls:MetroWindow x:Class="WpfApp6.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:WpfApp6"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="300">

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

Material Design In XAML ToolkitとMahApps.Metroを用いたマテリアルデザイン風Window の続き)

Material Design In XAML Toolkitのデザイン設定は、モードとカラーがある

●モード
( Light / Dark )

・App.xaml

<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />

・プログラム内から動的変更

// Lightの場合はfalse、Darkの場合はtrue
new PaletteHelper().SetLightDark(true);

●カラー
( Amber / Blue / BlueGrey / Brown / Cyan / DeepOrange / DeepPurple /
Green / Grey / Indigo / LightBlue / LightGreen / Lime / Orange / Pink /
Purple / Red / Teal / Yellow )

・App.xaml

<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Indigo.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml" />

・プログラム内から動的変更

new PaletteHelper().ReplacePrimaryColor("Amber");
new PaletteHelper().ReplaceAccentColor("LightGreen");

なお、名前の一覧は MaterialDesignColors.SwatchesProvider にて取得可能。

MahApps.Metroにもモードとカラーがある

●モード
( BaseLight / BaseDark )
●カラー
( Red / Green / Blue / Purple / Orange / Lime / Emerald / Teal / Cyan /
Cobalt / Indigo / Violet / Pink / Magenta / Crimson / Amber / Yellow /
Brown / Olive / Steel / Mauve / Taupe / Sienna )

        public MainWindow()
        {
            InitializeComponent();

            ThemeManager.ChangeAppStyle(
                Application.Current,
                ThemeManager.GetAccent("Sienna"),
                ThemeManager.GetAppTheme("BaseLight")
                );
        }

MahApps.Metroのスタイルの詳細はこちらを参照

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

Material Design In XAML Toolkitを用いることで、簡単にWindowをマテリアルデザイン風にすることができる。
さらにMahApps.Metroを導入することで、細かいカスタマイズや拡張コントロールを利用できる。

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

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

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

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

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

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

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>