private IViewModelWrapper CreateViewModelGrid(IView view, object viewModelSource, WrapOptions wrapOptions)
{
var content = GetContent(view) as FrameworkElement;
if (content == null)
{
return null;
}
object tempObj = null;
if (_weakIsWrappingTable.TryGetValue(view, out tempObj))
{
return null;
}
var viewTypeName = view.GetType().Name;
_weakIsWrappingTable.Add(view, new object());
Grid vmGrid = null;
var existingGrid = GetContent(view) as Grid;
if (existingGrid != null)
{
if (existingGrid.Name.StartsWith(InnerWrapperName))
{
Log.Debug($"No need to create content wrapper grid for view model for view '{viewTypeName}', custom grid with special name defined");
vmGrid = existingGrid;
}
}
if (vmGrid == null)
{
Log.Debug($"Creating content wrapper grid for view model for view '{viewTypeName}'");
vmGrid = new Grid();
vmGrid.Name = InnerWrapperName.GetUniqueControlName();
#if NET || SL5
if (Enum<WrapOptions>.Flags.IsFlagSet(wrapOptions, WrapOptions.CreateWarningAndErrorValidatorForViewModel))
{
var warningAndErrorValidator = new WarningAndErrorValidator();
warningAndErrorValidator.SetBinding(WarningAndErrorValidator.SourceProperty, new Binding());
vmGrid.Children.Add(warningAndErrorValidator);
}
#endif
if (Enum<WrapOptions>.Flags.IsFlagSet(wrapOptions, WrapOptions.TransferStylesAndTransitionsToViewModelGrid))
{
content.TransferStylesAndTransitions(vmGrid);
}
SetContent(view, null);
vmGrid.Children.Add(content);
SetContent(view, vmGrid);
Log.Debug($"Created content wrapper grid for view model for view '{viewTypeName}'");
}
var binding = vmGrid.GetBindingExpression(FrameworkElement.DataContextProperty);
if (binding == null)
{
vmGrid.SetBinding(FrameworkElement.DataContextProperty, new Binding
{
Path = new PropertyPath("ViewModel"),
Source = viewModelSource
});
}
return new ViewModelWrapper(vmGrid);
}