반응형
1.샘플이미지
2.디자인 소스코드
<UserControl x:Class="YearMonthSelect.yearmonthDatapicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:YearMonthSelect">
<StackPanel Orientation="Horizontal">
<DatePicker Name="dpYearMonth" Grid.Column="1" SelectedDate="{Binding SelectedDate, Mode=TwoWay}" CalendarOpened="DatePicker_Opened" Width="100" HorizontalAlignment="Left">
<DatePicker.Resources>
<Style TargetType="DatePickerTextBox">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="txtYearMonth" Text="{Binding Path=SelectedDate, StringFormat = {}{0:yyyy/MM}, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" TextChanged="txtYearMonth_TextChanged" LostFocus="txtYearMonth_LostFocus" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.Resources>
</DatePicker>
</StackPanel>
</UserControl>
3. 프로그램 소스코드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace YearMonthSelect
{
/// <summary>
/// Interaction logic for yearmonthDatapicker.xaml
/// </summary>
public partial class yearmonthDatapicker : UserControl
{
/// <summary>
/// 년월값 변수
/// </summary>
private string _Text { get; set; }
public string Text
{
get { return _Text; } // get method
set { _Text = value; } // set method
}
public yearmonthDatapicker()
{
InitializeComponent();
}
/// <summary>
/// DatePicker달력을열때의이벤트
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DatePicker_Opened(object sender, RoutedEventArgs e)
{
//DataPicker달력컨트롤취득
DatePicker datepicker = (DatePicker)sender;
//DatePickerTextBox을Popup형식으로변환(PART_Popup은고정)
Popup popup = (Popup)datepicker.Template.FindName("PART_Popup", datepicker);
//Popup을 달력의 하위컨트롤로등록
Calendar cal = (Calendar)popup.Child;
//달력화면모드 변경이벤트선언
cal.DisplayModeChanged += Calender_DisplayModeChanged;
//달력을12개월씩 년단위로 표시
cal.DisplayMode = CalendarMode.Year;
}
/// <summary>
/// 월선택 이벤트
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Calender_DisplayModeChanged(object sender, CalendarModeChangedEventArgs e)
{
Calendar calendar = (Calendar)sender;
if (calendar.DisplayMode == CalendarMode.Month)
{
//달력에서 선택한 년월값갱신
calendar.SelectedDate = calendar.DisplayDate;
//일자를선택하지 않고 달력화면을 닫음
dpYearMonth.IsDropDownOpen = false;
}
}
/// <summary>
/// 년월변경이벤트
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtYearMonth_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox txtKeijoMonth = (TextBox)sender;
if(txtKeijoMonth != null)
{
this._Text = txtKeijoMonth.Text;
}
}
/// <summary>
/// 달력에서포커스가 사라졌을때의 이벤트
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtYearMonth_LostFocus(object sender, RoutedEventArgs e)
{
//Datapicker달력을다시열수있도록 설정
dpYearMonth.IsDropDownOpen = true;
}
}
}
4. 샘플프로젝트
반응형
'WPF' 카테고리의 다른 글
TextBlock의 Text의 Inlines 추가 Text의 색상지정 (0) | 2022.02.24 |
---|---|
DataGrid의 DataGridTemplateColumn안에서 Control (TextBlock)찾기 (0) | 2022.02.24 |
ListBox의 CheckBox가져오기 (0) | 2021.08.20 |
DataGrid의 ComboBox바인딩 샘플 (0) | 2021.04.10 |
WPF 천단위 숫자입력 TextBox UserControl작성 (0) | 2021.02.27 |
댓글