Silverlight 4 MVVM:在自定义日期时间控件中绑定 SelectedDate 会导致错误

发布于 2024-10-22 02:10:25 字数 2700 浏览 2 评论 0原文

我有一个自定义日期时间控件。 当我绑定其 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>

视图:

ViewModel:

    public DateTime? DiarySelectedDate
    {
        get
        {
            return _selectedDate;
        }
        set
        {
            if (_selectedDate == value)
            {
                return;
            }
            _selectedDate = value;
            RaisePropertyChanged("DiarySelectedDate");
        }
    }

非常感谢任何帮助, 提前致谢, 克鲁维

i have a custom datetime control.
When i bind its SelectedDate property it result with the following error:

System.Windows.Markup.XamlParseException: Set property 'GenericControls.CustomDatePickerControl.SelectedDate' threw an exception. [Line: 44 Position: 131] ---> System.ArgumentException: Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Nullable`1[System.DateTime]'.

The code behind:

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
}

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

The view:

The ViewModel:

    public DateTime? DiarySelectedDate
    {
        get
        {
            return _selectedDate;
        }
        set
        {
            if (_selectedDate == value)
            {
                return;
            }
            _selectedDate = value;
            RaisePropertyChanged("DiarySelectedDate");
        }
    }

Any help is much appreciated,
Thanks in advance,
Kruvi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九八野马 2024-10-29 02:10:25

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

但我想通知一下我最近开始创建的 Silverlight 5 控制工具包的此链接。它包含(以及其他控件)一个 DateTimeBox 控件,您可以在同一控件中处理日期和时间。目前,它仍在开发中,但应该可以用于大多数场景。

I notice this is an answered question.

But I would like to notify about this link to a Silverlight 5 control toolkit I have recently started creating. It contains (among other controls) a DateTimeBox control in which you can handle both date and time within the same control. At this point, it is still under development, but it should be usable for most scenarios.

你穿错了嫁妆 2024-10-29 02:10:25

SelectedDate 必须是依赖属性。 MSDN

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

SelectedDate must be dependency property. MSDN

Update
If you want that will work change declare SelectedDate as dependency property and OnSelectedChanged (invoked on SelectedDate dependency property changed) set NewValue to dtpFromDate control in layout. I'm not sure that it will be good design approach but it solve your problem.

谈下烟灰 2024-10-29 02:10:25

有什么理由不能只在 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>

Any reason you can't just do the date binding in 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文