System.Windows.Controls.ViewModelAttribute.GetCurrentViewModel C# (CSharp) Method

GetCurrentViewModel() public static method

Gets the model associated with the specified framework element. This will walk up the parent hierarchy to find a model if the specified element does not have a model.
public static GetCurrentViewModel ( FrameworkElement element ) : object
element System.Windows.FrameworkElement The element to lookup.
return object
        public static object GetCurrentViewModel(FrameworkElement element)
        {
            object model = element.GetValue(ViewModelProperty);

            if (model == null) {
                element = element.GetParentVisual();
                while (element != null) {
                    model = element.GetValue(ViewModelProperty);
                    if (model != null) {
                        break;
                    }

                    element = element.GetParentVisual();
                }
            }

            if (model == null) {
                IApplicationContext appContext = null;
                if (ComponentContainer.Global != null) {
                    appContext = ComponentContainer.Global.GetObject<IApplicationContext>();
                }

                if (appContext != null) {
                    model = appContext.Model;
                }
            }

            return model;
        }