7

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

user7116
  • 63,008
  • 17
  • 141
  • 172
Robin
  • 555
  • 4
  • 14
  • Try to reproduce in a mini App. Either you find the problem or you have something to post here. – H H May 26 '11 at 11:05
  • Good idea, can't believe I didn't even consider that.. But anyway, I managed to duplicate the exact same error with a minimum amount of code in a brand new project. Odd thing is, if I choose to ignore the exception and continue running, it is not thrown again and I can change month just fine. (Maybe VS remembers my choice to continue?) XAML: https://gist.github.com/993005 C#: https://gist.github.com/993003 – Robin May 26 '11 at 12:09
  • 2
    I moved your code into the actual question in case github dies today. – user7116 May 26 '11 at 13:41
  • Can't reproduce (VS2010, Fx4 Client Profile). – H H May 26 '11 at 19:48
  • Ok I tried running the project without debugging, seemed to work fine (or it ignored the exceptions). This after repairing both .NET and Visual Express.. Tried running the project in VS 2010 Ultimate, both with and without debugging, and it worked just fine.. Seems it might have something to do with the Express version, I'll try it on another computer and see how that goes. – Robin May 27 '11 at 09:40
  • Is your Date property a DependencyProperty? It may be that the binding is failing, but only when it tries to update the source. – Geoff Cox Jul 26 '11 at 16:47
  • @Robin I'm seeing the same behavior, but it didn't start occurring until recently after a couple months of it working fine. I was relieved to see that your reproduction was the bare minimum since my implementation is heavily modified. I was dreading a re-design. I am curious to know if you ever found a fix, as I'm near a deadline and can't waste too much time playing detective. Thanks! – erodewald Apr 18 '12 at 22:31

1 Answers1

0

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.

ShelbyZ
  • 1,494
  • 1
  • 14
  • 32