Skip to content

Fails to Provide Critical Information #3716

@markolbert

Description

@markolbert

I am struggling with moving from Binding to x:Bind. The discussion about how you need to define a viewmodel in your control's code is way too vague. Specifically, it fails to address how to handle the situation where a control is embedded within another control and its DataContext is therefore set at runtime.

In my control code-behind I've declared this:

        private ILocation ViewModel { get; set; } = new Location();

        private void LocationControl_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
        {
            if( args.NewValue is not ILocation viewModel )
                return;

            ViewModel = viewModel;
        }

The event handler gets called multiple times with "null" values for NewValue. It then gets called with a valid ILocation value, updates the property ViewModel... and the UI proudly displays absolutely nothing (i.e., the default values from new Location(), which is of course useless).

I'm not sure why this is happening. But I think you ought to expand the discussion to do a better job explaining how to get this stuff to work.

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @markolbert@jwmsft@PRMerger8

      Issue actions

        Fails to Provide Critical Information · Issue #3716 · MicrosoftDocs/windows-dev-docs