質問するログイン新規登録
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

2回答

6422閲覧

DatePickerで選択した日付を、12/07/2017と英語圏表記で表示したいです。

cancat

総合スコア313

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2017/07/12 07:37

0

0

こんにちは。
Windows10でWPFのMVVMアプリケーションを開発しています。
Visual Studio 2017 Communityを使っています。

###前提・実現したいこと
DatePickerで選択した日付を、12/07/2017と英語圏表記で表示したいです。

###発生している問題・エラーメッセージ
2017/07/12となっている。

###該当のソースコード

xaml

1<DatePicker DisplayDateStart="1990/01/01" DisplayDateEnd="2049/12/31" 2SelectedDate="{Binding Path= StartDate, StringFormat='{}{0:dd/MM/yyyy}'}"/> 3
private DateTime? startDate = null; public DateTime? StartDate { get { return startDate; } set { startDate = value; RaisePropertyChanged(()=> StartDate); } }

###補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Visual Studio Community 2017
Version 15.0.26228.9 D15RTWSVC
Microsoft .NET Framework
Version 4.6.01586
日本語Windows
です。
よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

DatePickerで選択した日付を、12/07/2017と英語圏表記で表示したいです。

アメリカとイギリスでは違うので、「英語圏表記」というのはあいまいですね。
dd/MM/yyyyとされているので、イギリス式(日月年)ということでしょうかね。
日付 - Wikipedia

<DatePicker Language="en-GB" />とするのが簡単です(ただしカレンダーも英語になります)
FrameworkElement.Language プロパティ (System.Windows) | Microsoft Learn

カレンダーは日本語にしたい場合は、CalendarLanguageを戻しますかね?(バカバカしいが^^;

xml

1<DatePicker Language="en-GB"> 2 <DatePicker.CalendarStyle> 3 <Style TargetType="Calendar"> 4 <Setter Property="Language" Value="ja-JP" /> 5 </Style> 6 </DatePicker.CalendarStyle> 7</DatePicker>

テンプレートをいじるのは自由度は高いですが、長くなるので最終手段ですかね。
DatePickerTextBoxControlTemplateは、実際にはもっと複雑です(わかりやすい差はWatermarkが出ない等)
.net - Custom WPF DatePickerTextBox Template - Stack Overflow

xml

1<Window 2 x:Class="Q83952.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:sys="clr-namespace:System;assembly=mscorlib" 6 Width="500" 7 Height="700"> 8 <StackPanel> 9 <GroupBox Margin="4" Header="Normal"> 10 <DatePicker SelectedDate="{x:Static sys:DateTime.Now}" SelectedDateFormat="Short" /> 11 </GroupBox> 12 13 <GroupBox Margin="4" Header="Language=&quot;en-GB&quot;"> 14 <DatePicker Language="en-GB" SelectedDate="{x:Static sys:DateTime.Now}" SelectedDateFormat="Short" /> 15 </GroupBox> 16 17 <GroupBox Margin="4" Header="Language=&quot;en-GB&quot;, Calendar.Language=&quot;ja-JP&quot;"> 18 <DatePicker Language="en-GB" SelectedDate="{x:Static sys:DateTime.Now}" SelectedDateFormat="Short"> 19 <DatePicker.CalendarStyle> 20 <Style BasedOn="{StaticResource {x:Type Calendar}}" TargetType="Calendar"> 21 <Setter Property="Language" Value="ja-JP" /> 22 </Style> 23 </DatePicker.CalendarStyle> 24 </DatePicker> 25 </GroupBox> 26 27 <GroupBox Margin="4" Header="DatePickerTextBox.Template"> 28 <DatePicker SelectedDate="{x:Static sys:DateTime.Now}"> 29 <DatePicker.Resources> 30 <Style TargetType="DatePickerTextBox"> 31 <Setter Property="Template"> 32 <Setter.Value> 33 <ControlTemplate> 34 <TextBox Text="{Binding SelectedDate, StringFormat='dd/MM/yyyy', ConverterCulture=en-GB, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" /> 35 </ControlTemplate> 36 </Setter.Value> 37 </Setter> 38 </Style> 39 </DatePicker.Resources> 40 </DatePicker> 41 </GroupBox> 42 </StackPanel> 43</Window>

アプリ動画

投稿2024/11/20 16:12

TN8001

総合スコア10166

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

How to change format (e.g. dd/MMM/yyyy) of DateTimePicker in WPF application
詳しく読んでないんで分かりませんが、ここらへんが参考になりませんか?

投稿2017/07/12 23:17

YAmaGNZ

総合スコア10681

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cancat

2017/07/13 01:24

ありがとうございます。 <DatePicker SelectedDate="{Binding Path=StartDate, StringFormat='{}{0:dd/MM/yyyy}'}"> <DatePicker.Resources> <Style TargetType="{x:Type DatePickerTextBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <TextBox Text="{Binding Path=StartDate, StringFormat='dd/MM/yyyy', RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </DatePicker.Resources> </DatePicker> として、該当のDateTimeConverterのクラスを作ってみましたが、文字を全く表示できませんでした。
YAmaGNZ

2017/07/13 13:16

私自身がWPFをほぼ分かっていないのですが、とりあえず Converterクラスは使わず <DatePicker HorizontalAlignment="Left" Height="29" Margin="44,42,0,0" VerticalAlignment="Top" Width="245"> <DatePicker.Resources> <Style TargetType="{x:Type DatePickerTextBox}"> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate> <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat='dd/MM/yyyy', RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" /> </ControlTemplate> </Setter.Value> </Setter> </Style> </DatePicker.Resources> </DatePicker> としたらとりあえず表示は変わりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問