Calendarを使う WPF
		
	 
	
	
	
	 
	
		
			Calendarを使う WPF
			日付を取得する時、カレンダーコントロールは有ったら便利なコントロール。 
			でも、グレープシティーのインプットマン的に全てお任せでやってくれるわけではない。 
			しかも、デイトピッカーはユーザインターフェイスが一般的なものとは違い、ちょっと使いにくいかも? 
			で、テキストボックスと、カレンダーを組み合わせたものもやってみましたが・・・ 
			個人的には、後者の方が扱いやすいし、好きだ。 
			因みに、StringFormat='yyyy年MM月dd日'を指定しておけば、テキストボックスのフォーカスが外れた時には、'yyyy年MM月dd日'に整形はしてくれる。 
			また、バインドエラーになると、テイストボックスが赤く縁取りされるみたいだ。 
			 
			デイトピッカー 
			  
			 
			
			 
			カレンダーコントロール
			
				
				ボタンをクリックして表示させた所。 
				 
				 | 
				
				フォーカスがはずれて、正しくない日付の場合 
			 
				 | 
			 
			 
			
			
			
			
			
			
			 
			Public Class Calendar 
			 
			    
			    Private Sub Button1_PreviewMouseDown(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs) Handles Button1.PreviewMouseDown 
			 
			        If IsDate(Me.t1.Text) = True Then 
			            calender1.SelectedDate = Me.t1.Text 
			        End If 
			 
			        calender1.Visibility = Windows.Visibility.Visible 
			 
			    End Sub 
			 
			    Private Sub calender1_SelectedDatesChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles calender1.SelectedDatesChanged 
			        Debug.Print(calender1.SelectedDate.ToString) 
			        With Me.calender1 
			            .Visibility = Windows.Visibility.Hidden 
			        End With 
			    End Sub 
	
			    Private Sub t1_LostFocus(sender As Object, e As System.Windows.RoutedEventArgs) Handles t1.LostFocus 
			          If IsDate(Me.t1.Text) = False Then 
			              MessageBox.Show("正しい日付を入力してください。", "注意", MessageBoxButton.OK, MessageBoxImage.Information) 
			          End If 
				    End Sub 
			End Class
 
  |  
  
<Window x:Class="Calendar"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Calendar" Height="355" Width="457" WindowStartupLocation="CenterScreen">
    <Grid>
        <StackPanel Margin="0,0,0,12" Name="root">
            
            <StackPanel Orientation="Horizontal">
                <TextBox Width="200" Text="{Binding Mode=TwoWay, ElementName=DatePicker1, Path=SelectedDate,StringFormat='yyyy年MM月dd日'}" />
                <DatePicker  Name="DatePicker1" Width="143" Height="33"  />
            </StackPanel>
            <StackPanel Orientation="Horizontal" >
                <TextBox Name="t1" Width="200" Height="25" Text="{Binding Mode=TwoWay, ElementName=calender1, Path=SelectedDate,StringFormat='yyyy年MM月dd日'}" />
                <Button Content="…" Height="25" Name="Button1" />
                <DatePicker  Name="DatePicker2" Width="0" Height="0" />
               
            </StackPanel>
             <Calendar Name="calender1" Visibility="Hidden" />
            
        </StackPanel>
        
    </Grid>
</Window>
			課題 
			入力制限が必要。数値だけ。 
			自動で、年月日が表示されて欲しい。 
			和暦にも対応。 
			要は、インプットマン的にしたいわけだ。
  
			
			面白そうで参考になる?ページ・サイト 
			
			
			
			
			
	 
	
		
		 
	
	 |