开发者

WPF DatePicker throws exception when changing month

Ok I have this project I'm working on in WPF (Visual C# 2010 Express), and I have a few DatePickers that are bound to DateTime-properties. Now if I just open up the "picker" and start changing months immediately there is no problem. However, if I select a date and then try to change the month, I get the following Exception:

Element does not exist or it is virtualized; use VirtualizedItem Pattern if it is supported.

The stack trace only shows:

[External Code]

my method that opens the dialog which contains the DatePickers (amongst other things)

[External Code]

If the properties are nullable or not doesn't seem to make a difference, and can't find a single result on any of the major search engines on 开发者_开发百科the above exception.

XAML

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <DatePicker SelectedDate="{Binding TheDate}" 
                    x:Name="datePicker1" />
        <Button Content="Button"
                x:Name="button1" 
                Click="button1_Click" />
    </Grid>
</Window>

Code Behind

    class TheClass
    {
        public DateTime TheDate { get; set; }
    }

    public MainWindow()
    {
        theClass = new TheClass();
        theClass.TheDate = DateTime.Now;

        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = theClass;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.MessageBox.Show(theClass.TheDate.ToString());
    }

Anyone ever seen this behaviour before or have any other ideas? I'm far from a WPF-expert and have honestly no idea what might be causing this or where to start looking..


I wish this could be a comment and not an answer but here goes:

http://www.switchonthecode.com/tutorials/wpf-snippet-detecting-binding-errors

Give that a read and it should report any binding errors if that is the issue in your case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜