WPF 사용자 정의 선택 연월 컨트롤 상세 설명
날짜 선택 컨트롤,XAML 코드 를 봉 인 했 습 니 다.
<UserControl x:Class="SunCreate.CombatPlatform.Client.DateMonthPicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="23" Loaded="UserControl_Loaded">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/SunCreate.CombatPlatform.Client.Resources;Component/Resource/DateTimePickerResource.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="ToggleButton" x:Key="stlToggleButton">
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="Back" Background="Transparent" BorderThickness="0" BorderBrush="Transparent">
<Path Name="PathFill" Fill="#1b94e0" Width="8" Height="6" StrokeThickness="0" Data="M5,0 L10,10 L0,10 z" RenderTransformOrigin="0.5,0.5" Stretch="Fill">
<Path.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="180"/>
<TranslateTransform/>
</TransformGroup>
</Path.RenderTransform>
</Path>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="PathFill" Property="Fill" Value="#1b94e0"></Setter>
<Setter TargetName="Back" Property="Background" Value="Transparent"></Setter>
<Setter TargetName="Back" Property="BorderBrush" Value="Transparent"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="ComboBox" x:Key="stlComboBox">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="HorizontalAlignment" Value="Left"></Setter>
<Setter Property="Foreground" Value="Black"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="Margin" Value="0,0,0,0"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="/SunCreate.CombatPlatform.Client.Resources;component/Image/Face/1 n / .png"/>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.7*"/>
<ColumnDefinition Width="0.3*" MaxWidth="30" MinWidth="18"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" IsReadOnly="True" Foreground="#1ba4f6" BorderThickness="1" BorderBrush="Transparent" Text="{TemplateBinding Text}" Background="Transparent"></TextBox>
<Border Grid.Column="0" BorderThickness="0" Background="Transparent">
</Border>
<Border Grid.Column="1" BorderThickness="0" CornerRadius="0,1,1,0" Background="Transparent">
<ToggleButton Style="{StaticResource stlToggleButton}" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"></ToggleButton>
</Border>
<Popup IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" PopupAnimation="Slide">
<Border CornerRadius="1" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True" Background="Transparent">
<Border.Effect>
<DropShadowEffect Color="#1ba4f6" BlurRadius="2" ShadowDepth="0" Opacity="0.5"/>
</Border.Effect>
<ScrollViewer Margin="4,6,4,6" Style="{DynamicResource ScrollViewerStyle}" MaxHeight="{TemplateBinding MaxDropDownHeight}" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True">
<!-- StackPanel , IsItemsHost True -->
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" Background="#1ba4f6"/>
</ScrollViewer>
</Border>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<ComboBox Grid.Column ="2" Grid.Row="0" Name="cbYear" SelectionChanged="cbYear_SelectionChanged" SelectedValuePath="Text" DisplayMemberPath="Text" Height="25" Width="55" Style="{StaticResource stlComboBox}" VerticalAlignment ="Center" >
</ComboBox>
<TextBlock Text=" " Margin="5 0 5 0" VerticalAlignment="Center" Foreground="#1ba4f6" />
<ComboBox Grid.Column ="2" Grid.Row="0" Name="cbMonth" SelectionChanged="cbMonth_SelectionChanged" SelectedValuePath="Text" DisplayMemberPath="Text" Height="25" Width="40" Style="{StaticResource stlComboBox}" VerticalAlignment ="Center" >
</ComboBox>
<TextBlock Text=" " Margin="5 0 5 0" VerticalAlignment="Center" Foreground="#1ba4f6" />
</StackPanel>
</Grid>
</UserControl>
배경 코드:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
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;
using System.ComponentModel;
namespace SunCreate.CombatPlatform.Client
{
/// <summary>
///
/// </summary>
public partial class DateMonthPicker : UserControl, INotifyPropertyChanged
{
private DateTime _selectedMonth;
public static DependencyProperty selectedTimeProperty;
static DateMonthPicker()
{
selectedTimeProperty = DependencyProperty.Register("SelectedMonth", typeof(DateTime), typeof(DateMonthPicker), new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(SelectedMonthChanged)));
}
public DateMonthPicker()
{
InitializeComponent();
int currentYear = DateTime.Now.Year;
int currentMonth = DateTime.Now.Month;
List<object> yearList = new List<object>();
for (int i = currentYear - 20; i <= currentYear; i++)
{
yearList.Add(new { Text = i.ToString() });
}
cbYear.ItemsSource = yearList;
cbMonth.ItemsSource = new List<object>() {
new { Text = "1" },
new { Text = "2" },
new { Text = "3" },
new { Text = "4" },
new { Text = "5" },
new { Text = "6" },
new { Text = "7" },
new { Text = "8" },
new { Text = "9" },
new { Text = "10" },
new { Text = "11" },
new { Text = "12" }};
this._selectedMonth = DateTime.Now;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
cbYear.SelectedValue = _selectedMonth.Year.ToString();
cbMonth.SelectedValue = _selectedMonth.Month.ToString();
}
private static void SelectedMonthChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
(obj as DateMonthPicker).ChangeSelect(e.NewValue);
}
private void ChangeSelect(object value)
{
_selectedMonth = (DateTime)value;
cbYear.SelectedValue = _selectedMonth.Year.ToString();
cbMonth.SelectedValue = _selectedMonth.Month.ToString();
}
public DateTime SelectedMonth
{
get { return (DateTime)this.GetValue(DateMonthPicker.selectedTimeProperty); }
set { this.SetValue(DateMonthPicker.selectedTimeProperty, value); }
}
public DateTime StartDay
{
get
{
return this._selectedMonth.AddDays(1 - this._selectedMonth.Day).Date;
}
}
public DateTime EndDay
{
get
{
return this.StartDay.AddMonths(1).AddDays(-1);
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void SendPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
private void cbYear_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cb = sender as ComboBox;
if (this._selectedMonth != DateTime.MinValue && cb.SelectedValue != null)
{
this._selectedMonth = new DateTime(Convert.ToInt32(cb.SelectedValue), this._selectedMonth.Month, 1);
SelectedMonth = this._selectedMonth;
}
}
private void cbMonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cb = sender as ComboBox;
if (this._selectedMonth != DateTime.MinValue && cb.SelectedValue != null)
{
this._selectedMonth = new DateTime(this._selectedMonth.Year, Convert.ToInt32(cb.SelectedValue), 1);
SelectedMonth = this._selectedMonth;
}
}
}
}
효과 그림:이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
MaterialDesign의 ComboBox HasClearButton 크기 변경WPF MaterialDesign은 편리하지만 때로는 표시가 너무 크거나 약간 사용하기 쉽습니다. ComboBox를 사용할 때 선택한 버튼을 지우려면 지우기 버튼을 표시할 수 있습니다. 아래와 같은 표시가 됩니다 다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.