Utsav Dawn
Utsav Dawn

Reputation: 8246

Cannot add instance of type '%0' to a collection of type '%1' error in windows phone 8.1 DatePicker

Hey I am developing an app in windows phone 8.1 using the MVVM pattern. I want to get the date from the DatePicker on the DateChanged event in the viewModel. After running the program I am getting this error:

A first chance exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in App1.exe WinRT information: Cannot add instance of type '%0' to a collection of type '%1'. [Line: 117 Position: 97] An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in App1.exe but was not handled in user code WinRT information: Cannot add instance of type '%0' to a collection of type '%1'. [Line: 117 Position: 97] Additional information: The text associated with this error code could not be found.

My view is:

<DatePicker Grid.Row="1" Grid.Column="1"
                VerticalContentAlignment="Center"
                HorizontalContentAlignment="Center"
                HorizontalAlignment="Left"
                VerticalAlignment="Center" Margin="26,-0.333,0,0.5"
                Date="{Binding Dates, Mode=TwoWay}">
        <i:Interaction.Behaviors>
            <core:EventTriggerBehavior EventName="DateChanged">
                <core:InvokeCommandAction Command="{Binding InitializeExpenseListCommand}"/>
            </core:EventTriggerBehavior>
        </i:Interaction.Behaviors>
    </DatePicker>

And the viewModel:

    public MainViewModel()
    {
        _dates = new DateTimeOffset(DateTime.Now);
    }

    private DateTimeOffset _dates;
    public DateTimeOffset Dates
    {
        get { return _dates; }
        set
        {
            _dates = value;
            RaisePropertyChanged();
        }
    }
    public ICommand InitializeExpenseListCommand
    {
        get { return new RelayCommand(InitializeExpenseList()); }
    }

    public Action InitializeExpenseList()
    {
        return () => Debug.WriteLine(_dates);
    }

Can anyone help me in solving this error?

Upvotes: 3

Views: 1294

Answers (1)

J&#252;rgen Bayer
J&#252;rgen Bayer

Reputation: 3023

My answer might be a bit late but this error (still) occurs (in WinRT 8.1 still with placeholders in the message!) if the event you try to add either does not exist or is not supported by the EventTriggerBehaviour. Supported events are: Tapped, PointerPressed, Loaded, DataContextChanged, Click, Checked, Unchecked, SelectionChanged, TextChanged, Toggled, NavigationCompleted

See https://msdn.microsoft.com/en-us/library/windows/apps/dn469361.aspx

Upvotes: 3

Related Questions