wpf自定义用户控件

时间:2011-08-29 12:51:57

标签: c# wpf xaml user-controls

  

可能重复:
  Wpf custom datepicker user control

我想创建一个用户控件,用于从用户获取具有3个文本框的日期,月份和日期,但我不知道如何创建它。请帮帮我

<UserControl x:Class="UI.WPF.CustomControls.ChooseDateControl"
         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" 
         mc:Ignorable="d" 
         x:Name="chooseDateControl"
         xmlns:custom="clr-namespace:UI.WPF.CustomControls"
         d:DesignHeight="26" d:DesignWidth="181" FontFamily="Tahoma">

<DockPanel LastChildFill="True">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1.5*" />
            <ColumnDefinition Width="14" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="14" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>

        <Label Content="/" Grid.Column="1" />
        <Label Content="/" Grid.Column="3" />

        <custom:NumericTextBox x:Name="txtYear" ToolTip="سال" Text="{Binding ElementName=chooseDateControl, Mode=TwoWay, Path=Year}" MaxLength="4" TabIndex="2" MinWidth="20" />
        <custom:NumericTextBox x:Name="txtMonth" Grid.Column="2" ToolTip="ماه" Text="{Binding ElementName=chooseDateControl, Mode=TwoWay, Path=Month}" MaxLength="2" TabIndex="1" MinWidth="20" />
        <custom:NumericTextBox x:Name="txtDay" Grid.Column="4" ToolTip="روز" Text="{Binding ElementName=chooseDateControl, Mode=TwoWay, Path=Day}" MaxLength="2" TabIndex="0" MinWidth="20" />
    </Grid>
</DockPanel>

这是背后的代码:

public partial class ChooseDateControl : UserControl
{
    public static readonly DependencyProperty ValueProperty;
    public static readonly DependencyProperty YearProperty;
    public static readonly DependencyProperty MonthProperty;
    public static readonly DependencyProperty DayProperty;

    static ChooseDateControl()
    {
        ValueProperty = DependencyProperty.Register(
            "Value",
            typeof(DateTime), typeof(ChooseDateControl),
            new FrameworkPropertyMetadata(DateTime.MinValue));

        ValueProperty = DependencyProperty.Register(
            "Year",
            typeof(int), typeof(ChooseDateControl),
            new FrameworkPropertyMetadata((int)0, new PropertyChangedCallback(OnYearChanged)));

        ValueProperty = DependencyProperty.Register(
            "Month",
            typeof(int), typeof(ChooseDateControl),
            new FrameworkPropertyMetadata((int)0, new PropertyChangedCallback(OnMonthChanged)));

        ValueProperty = DependencyProperty.Register(
            "Day",
            typeof(int), typeof(ChooseDateControl),
            new FrameworkPropertyMetadata((int)0, new PropertyChangedCallback(OnDayChanged)));
    }

    public ChooseDateControl()
    {
        InitializeComponent();
    }

    private static void OnDayChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        DateTime newDate = (DateTime)e.NewValue;
        DateTime oldDate = (DateTime)e.OldValue;

        NumericTextBox dayPicker = (NumericTextBox)sender;
        dayPicker.Text = newDate.Day.ToString();
    }
    private static void OnMonthChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        DateTime newDate = (DateTime)e.NewValue;
        DateTime oldDate = (DateTime)e.OldValue;

        NumericTextBox monthPicker = (NumericTextBox)sender;
        monthPicker.Text = newDate.Month.ToString();
    }
    private static void OnYearChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        DateTime newDate = (DateTime)e.NewValue;
        DateTime oldDate = (DateTime)e.OldValue;

        NumericTextBox yearPicker = (NumericTextBox)sender;
        yearPicker.Text = newDate.Year.ToString();
    }

    public DateTime Value
    {
        get { return (DateTime)base.GetValue(ValueProperty); }
        set { base.SetValue(ValueProperty, value); }
    }

    public int Year
    {
        get { return (int)base.GetValue(YearProperty); }
        set { base.SetValue(YearProperty, value); }
    }

    public int Month
    {
        get { return (int)base.GetValue(MonthProperty); }
        set { base.SetValue(MonthProperty, value); }
    }

    public int Day
    {
        get { return (int)base.GetValue(DayProperty); }
        set { base.SetValue(DayProperty, value); }
    }
}

它无法正常工作并返回默认值DateTime.MinValue。 我知道这是错的。请帮帮我

0 个答案:

没有答案