LoginSignup
0

More than 5 years have passed since last update.

Visual Studio / WPF > DataGrid > 2列だけ色を変える > XAML | TabIndex使用

Last updated at Posted at 2017-06-11
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community

http://qiita.com/7of9/items/e7c0a2b013a8aafc1424
の続き。

セルの二列だけ色を変えたい。

https://msdn.microsoft.com/ja-jp/library/system.windows.controls.datagridrow(v=vs.110).aspx
にてTabIndexを見つけた。

XAMLファイルにてTabIndexを使って実装してみた。

test.csv

アプリケーション実行ファイル生成フォルダに以下のファイルを用意する。

test.csv
Name1,Race1,Codename1
7of9,Borg,seven
Janeway,Human,Captain
Odo,Unknown,Odo

code

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
//
using System.IO;
using System.Globalization;
using System.Data;

namespace _170611_t1030_readCsvFile
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void B_read_Click(object sender, RoutedEventArgs e)
        {
            string appPath = System.AppDomain.CurrentDomain.BaseDirectory;
            string filename = "test.csv";
            string filepath = appPath + "\\" + filename;
            if (File.Exists(filepath) == false) {
                return;
            }
            this.DataContext = PersonService.ReadFile(filepath);
            PersonService.SetHeaders(filepath, dg1);
        }
    }

    // Name, Race, Codename
    public class Person
    {
        public string Name { get; set; }
        public string Race { get; set; }
        public string Codename { get; set; }
    }

    public static class PersonService
    {
        public static List<Person> ReadFile(string filepath)
        {
            var lines = File.ReadAllLines(filepath);

            var data = from l in lines.Skip(1)
                       let split = l.Split(',')
                       select new Person
                       {
                           Name = split[0],
                           Race = split[1],
                           Codename = split[2]
                       };
            return data.ToList();
        }
        public static void SetHeaders(string filepath, DataGrid dataGrid)
        {
            var lines = File.ReadAllLines(filepath);

            int idx = 0;
            foreach(var aline in lines[0].Split(','))
            {
                dataGrid.Columns[idx].Header = aline;
                idx++;
            }
        }
    }
}
MainWindow.xaml
<Window x:Class="_170611_t1030_readCsvFile.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:_170611_t1030_readCsvFile"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="keyCellStyle" TargetType="DataGridCell">
            <Style.Triggers>
                <Trigger Property="TabIndex" Value="0">
                    <Setter Property="Background" Value="LightGreen"/>
                </Trigger>
                <Trigger Property="TabIndex" Value="1">
                    <Setter Property="Background" Value="LightGreen"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button x:Name="B_read" Content="Read"
                    Click="B_read_Click" Height="28" Width="100"/>
            <DataGrid Height="300" x:Name="dg1"
                      AutoGenerateColumns="True"
                      IsReadOnly="true"
                      CellStyle="{StaticResource keyCellStyle}"
                      ItemsSource="{Binding}">
            </DataGrid>
        </StackPanel>
    </Grid>
</Window>

2017-06-11_14h51_40.png

選択時にも3列目もきちんと文字が読める。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0