作成日: 2014/10/22 最終更新日: 2014/10/22
文書種別
使用方法
詳細
日付を和暦で表示する場合は.NETのJapaneseCalendarクラスを使用します。
DataGridで使用する場合は、日付を和暦に変換する値コンバーターを定義しておき、BindingオブジェクトのConverterプロパティに設定します。
また、非編集時だけでなく、編集時にも和暦を扱う場合は入力に使用するエディタコントロールが和暦をサポートする必要があるため、DataGridでTemplateColumnを使用し、編集時に使用するエディタをカスタマイズして和暦による編集を可能にする方法があります。
以下のサンプルではDataGridのDataGridDateTimeColumn列を使用して和暦表示、DataGridTemplateColumnを使用して和暦の編集列を設定しています。
日付入力用のコントロールとしては、DataGridTemplateColumnのCellEditingTemplateとして、C1DatePickerを指定しています。
◎サンプルコード(XAML)
◎サンプルコード(Visual Basic)
◎サンプルコード(C#)
DataGridで使用する場合は、日付を和暦に変換する値コンバーターを定義しておき、BindingオブジェクトのConverterプロパティに設定します。
また、非編集時だけでなく、編集時にも和暦を扱う場合は入力に使用するエディタコントロールが和暦をサポートする必要があるため、DataGridでTemplateColumnを使用し、編集時に使用するエディタをカスタマイズして和暦による編集を可能にする方法があります。
以下のサンプルではDataGridのDataGridDateTimeColumn列を使用して和暦表示、DataGridTemplateColumnを使用して和暦の編集列を設定しています。
日付入力用のコントロールとしては、DataGridTemplateColumnのCellEditingTemplateとして、C1DatePickerを指定しています。
◎サンプルコード(XAML)
<!--必要に応じてWindow.Resources等に変更してください-->
<UserControl.Resources>
<local:JapaneseCalendarConverter x:Key="jpConverter" />
</UserControl.Resources>
<Grid Loaded="Grid_Loaded">
<c1:C1DataGrid AutoGenerateColumns="False" Name="C1DataGrid1">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="名前" Binding="{Binding Name}"/>
<c1:DataGridTextColumn Header="住所" Binding="{Binding Address}"/>
<c1:DataGridDateTimeColumn Header="入社日付" Binding="{Binding i, Converter={StaticResource jpConverter}}" DateFormat="Long" />
<c1:DataGridTemplateColumn Header="入社日付(和暦入力)">
<c1:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding i, Converter={StaticResource jpConverter}}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</DataTemplate>
</c1:DataGridTemplateColumn.CellTemplate>
<c1:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<c1:C1DatePicker SelectedDate="{Binding i}" CustomFormat="ggy年MM月dd日" />
</DataTemplate>
</c1:DataGridTemplateColumn.CellEditingTemplate>
</c1:DataGridTemplateColumn>
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
</Grid>
<UserControl.Resources>
<local:JapaneseCalendarConverter x:Key="jpConverter" />
</UserControl.Resources>
<Grid Loaded="Grid_Loaded">
<c1:C1DataGrid AutoGenerateColumns="False" Name="C1DataGrid1">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="名前" Binding="{Binding Name}"/>
<c1:DataGridTextColumn Header="住所" Binding="{Binding Address}"/>
<c1:DataGridDateTimeColumn Header="入社日付" Binding="{Binding i, Converter={StaticResource jpConverter}}" DateFormat="Long" />
<c1:DataGridTemplateColumn Header="入社日付(和暦入力)">
<c1:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding i, Converter={StaticResource jpConverter}}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</DataTemplate>
</c1:DataGridTemplateColumn.CellTemplate>
<c1:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<c1:C1DatePicker SelectedDate="{Binding i}" CustomFormat="ggy年MM月dd日" />
</DataTemplate>
</c1:DataGridTemplateColumn.CellEditingTemplate>
</c1:DataGridTemplateColumn>
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
</Grid>
◎サンプルコード(Visual Basic)
Partial Public Class KB81011
Inherits UserControl
Public Sub New()
Dim jpCulture As Globalization.CultureInfo = New Globalization.CultureInfo("ja-JP", True)
jpCulture.DateTimeFormat.Calendar = New Globalization.JapaneseCalendar()
jpCulture.DateTimeFormat.LongDatePattern = "ggyy年M月d日"
System.Threading.Thread.CurrentThread.CurrentCulture = jpCulture
Me.Language = Markup.XmlLanguage.GetLanguage(Globalization.CultureInfo.CurrentCulture.Name)
InitializeComponent()
End Sub
Private person As Collections.ObjectModel.ObservableCollection(Of Person)
Private Sub Grid_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
person = New Collections.ObjectModel.ObservableCollection(Of Person)() From
{
New Person() With {.Name = "佐藤", .Address = "宮城", .i = New DateTime(2014, 2, 22)},
New Person() With {.Name = "木村", .Address = "東京", .i = New DateTime(2014, 9, 11)},
New Person() With {.Name = "高橋", .Address = "大阪", .i = New DateTime(2014, 9, 26)},
New Person() With {.Name = "斎藤", .Address = "福岡", .i = New DateTime(2014, 6, 12)}
}
C1DataGrid1.ItemsSource = person
End Sub
End Class
Public Class Person
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Address() As String
Get
Return m_Address
End Get
Set(ByVal value As String)
m_Address = value
End Set
End Property
Private m_Address As String
Public Property i() As DateTime
Get
Return i_
End Get
Set(ByVal value As DateTime)
i_ = value
End Set
End Property
Private i_ As DateTime
End Class
Public Class JapaneseCalendarConverter
Implements Windows.Data.IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements Windows.Data.IValueConverter.Convert
Return CType(value, DateTime).ToString("ggyy年M月d日")
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements Windows.Data.IValueConverter.ConvertBack
Return value
End Function
End Class
Inherits UserControl
Public Sub New()
Dim jpCulture As Globalization.CultureInfo = New Globalization.CultureInfo("ja-JP", True)
jpCulture.DateTimeFormat.Calendar = New Globalization.JapaneseCalendar()
jpCulture.DateTimeFormat.LongDatePattern = "ggyy年M月d日"
System.Threading.Thread.CurrentThread.CurrentCulture = jpCulture
Me.Language = Markup.XmlLanguage.GetLanguage(Globalization.CultureInfo.CurrentCulture.Name)
InitializeComponent()
End Sub
Private person As Collections.ObjectModel.ObservableCollection(Of Person)
Private Sub Grid_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
person = New Collections.ObjectModel.ObservableCollection(Of Person)() From
{
New Person() With {.Name = "佐藤", .Address = "宮城", .i = New DateTime(2014, 2, 22)},
New Person() With {.Name = "木村", .Address = "東京", .i = New DateTime(2014, 9, 11)},
New Person() With {.Name = "高橋", .Address = "大阪", .i = New DateTime(2014, 9, 26)},
New Person() With {.Name = "斎藤", .Address = "福岡", .i = New DateTime(2014, 6, 12)}
}
C1DataGrid1.ItemsSource = person
End Sub
End Class
Public Class Person
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Address() As String
Get
Return m_Address
End Get
Set(ByVal value As String)
m_Address = value
End Set
End Property
Private m_Address As String
Public Property i() As DateTime
Get
Return i_
End Get
Set(ByVal value As DateTime)
i_ = value
End Set
End Property
Private i_ As DateTime
End Class
Public Class JapaneseCalendarConverter
Implements Windows.Data.IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements Windows.Data.IValueConverter.Convert
Return CType(value, DateTime).ToString("ggyy年M月d日")
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements Windows.Data.IValueConverter.ConvertBack
Return value
End Function
End Class
◎サンプルコード(C#)
public partial class KB81011 : UserControl
{
public KB81011()
{
CultureInfo jpCulture = new CultureInfo("ja-JP", true);
jpCulture.DateTimeFormat.Calendar = new JapaneseCalendar();
jpCulture.DateTimeFormat.LongDatePattern = "ggyy年M月d日";
Thread.CurrentThread.CurrentCulture = jpCulture;
this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.Name);
InitializeComponent();
}
private ObservableCollection person;
void Grid_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
person = new ObservableCollection(){
new Person() {Name = "佐藤", Address="宮城", i = new DateTime(2014,2,22)},
new Person() {Name = "木村", Address="東京", i = new DateTime(2014,9,11)},
new Person() {Name = "高橋", Address="大阪", i = new DateTime(2014,9,26)},
new Person() {Name = "斎藤", Address="福岡", i = new DateTime(2014,6,12)},
};
C1DataGrid1.ItemsSource = person;
}
}
public class Person
{
public string Name {
get { return m_Name; }
set { m_Name = value; }
}
private string m_Name;
public string Address {
get { return m_Address; }
set { m_Address = value; }
}
private string m_Address;
public DateTime i {
get { return i_; }
set { i_ = value; }
}
private DateTime i_;
}
public class JapaneseCalendarConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((System.DateTime)value).ToString("ggyy年M月d日");
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
{
public KB81011()
{
CultureInfo jpCulture = new CultureInfo("ja-JP", true);
jpCulture.DateTimeFormat.Calendar = new JapaneseCalendar();
jpCulture.DateTimeFormat.LongDatePattern = "ggyy年M月d日";
Thread.CurrentThread.CurrentCulture = jpCulture;
this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.Name);
InitializeComponent();
}
private ObservableCollection
void Grid_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
person = new ObservableCollection
new Person() {Name = "佐藤", Address="宮城", i = new DateTime(2014,2,22)},
new Person() {Name = "木村", Address="東京", i = new DateTime(2014,9,11)},
new Person() {Name = "高橋", Address="大阪", i = new DateTime(2014,9,26)},
new Person() {Name = "斎藤", Address="福岡", i = new DateTime(2014,6,12)},
};
C1DataGrid1.ItemsSource = person;
}
}
public class Person
{
public string Name {
get { return m_Name; }
set { m_Name = value; }
}
private string m_Name;
public string Address {
get { return m_Address; }
set { m_Address = value; }
}
private string m_Address;
public DateTime i {
get { return i_; }
set { i_ = value; }
}
private DateTime i_;
}
public class JapaneseCalendarConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((System.DateTime)value).ToString("ggyy年M月d日");
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
旧文書番号
81011