かつて在りし日の Windows フォーム では、DataGridView が非常に人気があり、その中でも、行の背景を交互に変える事が特に人気でした。
具体的には、奇数行の背景色だけを設定するプロパティがあり、それが活用されていたものと思います。
Windows フォーム では AlternatingRowsDefaultCellStyle の BackColor を指定することで実装できました。
WPF ではもっと簡単に、DataGrid が AlternatingRowBackground というプロパティを持っています。
ここにお好みの色を設定すれば OK です。
■ コード
コードは次のようになります。
<Window x:Class="WpfApp1.Window1" 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="Window1" Height="300" Width="300"> <Grid> <DataGrid ItemsSource="{Binding}" AlternatingRowBackground="Azure"/> </Grid> </Window>
広告