Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Wpf自定义日期选择器用户控件_C#_Wpf_Xaml_User Controls_Xaml Binding - Fatal编程技术网

C# Wpf自定义日期选择器用户控件

C# Wpf自定义日期选择器用户控件,c#,wpf,xaml,user-controls,xaml-binding,C#,Wpf,Xaml,User Controls,Xaml Binding,我想创建一个用户控件,用于从用户处获取日期。它应该有三个文本框,一个用于年、月和日。我不知道如何创建它 <UserControl x:Class="UI.WPF.CustomControls.ChooseDateControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006

我想创建一个用户控件,用于从用户处获取日期。它应该有三个文本框,一个用于年、月和日。我不知道如何创建它

<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>

它不能正常工作-它返回默认值,即DateTime.MinValue。请帮帮我。

为什么不使用中的日历或日期选择器控件?您在问题中没有提到任何自定义的内容,因此这对您来说应该是可行的。

您没有将文本框连接到依赖项属性。该值从未设置过。您需要某种类型的转换器,将文本框中的值转换为实际日期。我认为最好使用toolkit 3.5或framework 4.0中的DatePicker控件。

对于初学者,停止复制和粘贴:0。您所有的DP注册都是针对ValueProperty的。您还必须为每个属性处理属性更改回调,以更新Value属性。

编写如下逻辑可能更好:

static void Main(string[] args)
{
    Console.WriteLine("Year");
    var year = Int32.Parse(Console.ReadLine());
    Console.WriteLine("Month");
    var month = Int32.Parse(Console.ReadLine());
    Console.WriteLine("Day");
    var day = Int32.Parse(Console.ReadLine());

    var customDate = new DateTime(year, month, day);

    Console.WriteLine(customDate);
    Console.ReadLine();
}

您可以实现类似的功能,比如有一个按钮,按下该按钮将获取值并创建一个新的日期时间值。从输入值生成日期时间的方法有很多,只要在网上冲浪,你就会在WPF中找到大量的例子和教程。希望这会给你一些想法并帮助你。

谢谢你的回答-因为datepicker没有波斯日期,还有其他原因。请帮忙。我编辑了代码,请再次查看。更新后的代码现在会发生什么?您是否已通过调试器进入控件?正在设置您的属性吗?您还可以使用Snoop查看是否存在任何绑定错误,并查看数据上下文设置为什么。结果是DateTime.MinValue您仍然需要修复财产注册。您正在将所有内容注册到ValueProperty。确保您更改为设置YearProperty、MonthProperty等。设置值的逻辑也存在一些问题,其中一个问题是您从未设置Value属性。您可能希望查看扩展WPF工具包中的DatTimeUpDown控件,以了解它是如何处理的:我建议您使用Value属性作为用于数据绑定的属性。这意味着您必须为Value属性处理属性changted call basck以设置其他属性值。在其他propert回调中,您可能希望使用新的D/M/Y组合设置Value属性。
static void Main(string[] args)
{
    Console.WriteLine("Year");
    var year = Int32.Parse(Console.ReadLine());
    Console.WriteLine("Month");
    var month = Int32.Parse(Console.ReadLine());
    Console.WriteLine("Day");
    var day = Int32.Parse(Console.ReadLine());

    var customDate = new DateTime(year, month, day);

    Console.WriteLine(customDate);
    Console.ReadLine();
}