Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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# Silverlight 4 MVVM:在自定义日期时间控件结果中绑定SelectedDate时出错_C#_.net_Silverlight_Xaml_Mvvm - Fatal编程技术网

C# Silverlight 4 MVVM:在自定义日期时间控件结果中绑定SelectedDate时出错

C# Silverlight 4 MVVM:在自定义日期时间控件结果中绑定SelectedDate时出错,c#,.net,silverlight,xaml,mvvm,C#,.net,Silverlight,Xaml,Mvvm,我有一个自定义日期时间控件。 当我绑定其SelectedDate属性时,结果是出现以下错误: System.Windows.Markup.XamlParseException:设置属性“GenericControls.CustomDatePickerControl.SelectedDate”引发异常。[行:44位置:131]-->System.ArgumentException:类型为“System.Windows.Data.Binding”的对象无法转换为类型为“System.Nullable

我有一个自定义日期时间控件。 当我绑定其SelectedDate属性时,结果是出现以下错误:

System.Windows.Markup.XamlParseException:设置属性“GenericControls.CustomDatePickerControl.SelectedDate”引发异常。[行:44位置:131]-->System.ArgumentException:类型为“System.Windows.Data.Binding”的对象无法转换为类型为“System.Nullable`1[System.DateTime]”的对象

背后的代码:

public partial class CustomDatePickerControl : UserControl
{
    #region Properties

    public DateTime? SelectedDate
    {
        get
        {
            return (DateTime?)dtpFromDate.GetValue
                                        (DatePicker.SelectedDateProperty);
        }
        set
        {
            dtpFromDate.SetValue(DatePicker.SelectedDateProperty, value);
        }
    }

    #endregion

    #region Initialization

    public CustomDatePickerControl()
    {
        InitializeComponent();
        Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        Binding binding = new Binding("Text");
        binding.Source = txtDate;
        binding.Mode = BindingMode.TwoWay;
        binding.Converter = (IValueConverter)Application.Current
                             .Resources["StringToDateConverter"];
        dtpFromDate.SetBinding(DatePicker.SelectedDateProperty, binding);            
    }

    #endregion

    #region EventHandlers

    private void txtDate_GotFocus(object sender, RoutedEventArgs e)
    {
        if (txtDate.Text == "To Date")
        {
            txtDate.Text = "";
        }
    }

    private void txtDate_LostFocus(object sender, RoutedEventArgs e)
    {
        if (txtDate.Text == "")
        {
            txtDate.Text = "From Date";
        }
    }

    #endregion
}
XAML:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <StackPanel Orientation="Horizontal" Background="Transparent">
        <TextBox x:Name="txtDate" Text="From Date:"  Width="74" Style="{StaticResource CommonTextBox}" Margin="3,0,0,0" LostFocus="txtDate_LostFocus" GotFocus="txtDate_GotFocus" MaxLength="10" />
        <sdk:DatePicker Name="dtpFromDate" Grid.Column="6" Height="23" Width="25" Style="{StaticResource DatePickerStyle}" />
    </StackPanel>
</Grid>
非常感谢您的帮助, 提前感谢,,
Kruvi

SelectedDate必须是依赖项属性

更新
如果需要,将更改声明SelectedDate为依赖项属性,并将OnSelectedChanged(在SelectedDate依赖项属性更改时调用)在布局中将NewValue设置为dtpFromDate控件。我不确定这是否是一种好的设计方法,但它解决了您的问题。

有什么原因不能用XAML进行日期绑定吗

<Grid x:Name="LayoutRoot" Background="Transparent">
    <StackPanel Orientation="Horizontal" Background="Transparent">
        <TextBox x:Name="txtDate" Text="From Date:"  Width="74" Style="{StaticResource CommonTextBox}" Margin="3,0,0,0" LostFocus="txtDate_LostFocus" GotFocus="txtDate_GotFocus" MaxLength="10" />
        <sdk:DatePicker SelectedDate="{Binding Path=DiarySelectedDate, Mode=TwoWay, Converter={StaticResource myConverterKey}}" Name="dtpFromDate" Grid.Column="6" Height="23" Width="25" Style="{StaticResource DatePickerStyle}" />
    </StackPanel>
</Grid>

我注意到这是一个已回答的问题


但我想通知大家,我最近开始创建一个Silverlight 5控件工具包。它包含(除其他控件外)一个DateTimeBox控件,您可以在该控件中处理同一控件中的日期和时间。目前,它仍在开发中,但在大多数情况下都应该可用。

我认为依赖项属性不能为空。请参见异常消息kruvi bind DiarySelectedDate从ViewModel到GenericControls.CustomDatePickerControl.SelectedDate属性中的内容。但在CustomDatePickerControl中,未将SelectedDate声明为依赖项属性。和绑定失败。原因是我在多个视图/视图模型中使用此控件,而只有一个视图模型中存在DiarySelectedDate。
<Grid x:Name="LayoutRoot" Background="Transparent">
    <StackPanel Orientation="Horizontal" Background="Transparent">
        <TextBox x:Name="txtDate" Text="From Date:"  Width="74" Style="{StaticResource CommonTextBox}" Margin="3,0,0,0" LostFocus="txtDate_LostFocus" GotFocus="txtDate_GotFocus" MaxLength="10" />
        <sdk:DatePicker SelectedDate="{Binding Path=DiarySelectedDate, Mode=TwoWay, Converter={StaticResource myConverterKey}}" Name="dtpFromDate" Grid.Column="6" Height="23" Width="25" Style="{StaticResource DatePickerStyle}" />
    </StackPanel>
</Grid>