[Silverlight] 和暦から西暦に変換する方法
19001 ワード
CultureInfoとJapaneseCalendarを利用すればよいらしい。
Window1.xaml
<controls:ChildWindow x:Class="SilverlightApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Width="377" Height="184"
Title="ChildWindow">
<Grid x:Name="LayoutRoot" Margin="2,2,2,1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Margin="2">
<TextBlock Text="元号" Margin="3"></TextBlock>
<Border BorderBrush="Black"
BorderThickness="5"
CornerRadius="8"
Margin="2"
>
<StackPanel Orientation="Horizontal">
<RadioButton GroupName="group1"
Content="M"
ToolTipService.ToolTip="明治"
Checked="RadioButton_Checked"
>
</RadioButton>
<RadioButton GroupName="group1"
Content="T"
ToolTipService.ToolTip="大正"
Checked="RadioButton_Checked"
>
</RadioButton>
<RadioButton GroupName="group1"
Content="S"
ToolTipService.ToolTip="昭和"
IsChecked="true"
Checked="RadioButton_Checked"
>
</RadioButton>
<RadioButton GroupName="group1"
Content="H"
ToolTipService.ToolTip="平成"
Checked="RadioButton_Checked"
>
</RadioButton>
</StackPanel>
</Border>
</StackPanel>
<StackPanel Orientation="Horizontal"
Grid.Row="1"
Margin="2">
<TextBlock
Margin="2"
Text="年">
</TextBlock>
<TextBox
x:Name="year"
Grid.Row="0"
Grid.Column="1"
Margin="2"
Text=""
MaxLength="2"
>
</TextBox>
<TextBlock
Grid.Row="1"
Grid.Column="0"
Margin="2"
Text="月">
</TextBlock>
<TextBox
x:Name="month"
Grid.Row="1"
Grid.Column="1"
Margin="2"
Text=""
MaxLength="2"
>
</TextBox>
<TextBlock
Grid.Row="2"
Grid.Column="0"
Margin="2"
Text="日">
</TextBlock>
<TextBox
x:Name="day"
Grid.Row="2"
Grid.Column="1"
Margin="2"
Text=""
MaxLength="2"
>
</TextBox>
</StackPanel>
<Button x:Name="CancelButton" Content="キャンセル" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="2" />
<Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="2" />
</Grid>
</controls:ChildWindow>
Window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication3
{
public partial class Window1 : ChildWindow
{
private readonly Dictionary<string, int> m_yearStrToMaxYearValue = new Dictionary<string, int>()
{
{"M", 45 },
{"T", 15 },
{"S", 64 },
{"H", 27 },
};
public Window1()
{
InitializeComponent();
}
private string m_yearStr;
private bool IsValid()
{
int max = m_yearStrToMaxYearValue[m_yearStr];
if (int.Parse(this.year.Text) > max)
{
MessageBox.Show("入力エラー");
return false;
}
return true;
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
CultureInfo culture = new CultureInfo("ja-JP")
{
DateTimeFormat = { Calendar = new JapaneseCalendar() }
};
if (!IsValid())
{
this.DialogResult = false;
return;
}
string target = String.Format("{0}{1}/{2}/{3}",
m_yearStr,
this.year.Text,
this.month.Text,
this.day.Text
);
var d = DateTime.Parse(target, culture);
this.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(d.ToString("yyyy/MM/dd"));
});
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
var cur = sender as RadioButton;
m_yearStr = cur.Content.ToString();
}
}
}
Author And Source
この問題について([Silverlight] 和暦から西暦に変換する方法), 我々は、より多くの情報をここで見つけました https://qiita.com/hidetzu/items/f3118d86cb3b39a55f41著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .