private static void BindProperties(CustomBasicList <FrameworkElement> controls, Type viewmodelType, IEnumerable <PropertyInfo> properties, Func <CustomBasicList <FrameworkElement>, PropertyInfo, CustomBasicList <FrameworkElement> > listFunct, Func <Type, BindHelpClass> bindInfo, Func <FrameworkElement, string> stringName ) { foreach (var property in properties) { var firstList = listFunct(controls, property); //var firstList = controls.Where(x => x.Name.StartsWith(property.Name, StringComparison.InvariantCultureIgnoreCase)).ToCustomBasicList(); //if (firstList.Count > 1) // throw new BasicBlankException($"{property.Name} showed 2 controls with the name. Only one is allowed for now for properties. If the problem was an _ then really requires rethinking. Most likely had wrong names. If I am wrong, rethink"); if (firstList.Count == 0) { continue; } //looks like we may have more than one now. that could be true for cases like dates. //where it needs to be displayed in more than one place. foreach (var foundControl in firstList) { if (foundControl == null) { continue; } DependencyProperty boundProperty; BindHelpClass helps = bindInfo.Invoke(foundControl.GetType()); if (helps.IsValid == false) { continue; //because we already have binding. } boundProperty = helps.Dependency !; //if (!_boundProperties.TryGetValue(foundControl.GetType(), out boundProperty!)) // continue; if (foundControl.GetBindingExpression(boundProperty) != null) { //because if it had to be done manually, don't want to override it. //this means i can do manually if necessary. continue; } var interpretedViewModelType = viewmodelType; var interpretedProperty = property; //not always name. var stringPath = stringName(foundControl); //var stringPath = foundControl.Name; stringPath = stringPath.Replace("_", "."); //i can try to do here. CustomBasicList <string> cList = stringPath.Split(".").ToCustomBasicList(); cList.RemoveFirstItem(); //i like this idea. foreach (var item in cList) { interpretedViewModelType = interpretedProperty.PropertyType; var nList = interpretedViewModelType.GetProperties(x => x.Name == item).ToCustomBasicList(); if (nList.Count == 0) { throw new BasicBlankException($"Unable to get the property for linked properties. The full string was {foundControl.Name}. The one unfound was {item}"); } if (nList.Count > 1) { throw new BasicBlankException($"There was more than one match for linked properties. The full string was {foundControl.Name}. The one with duplicate was {item}"); } interpretedProperty = nList.Single(); } var binding = new Binding(stringPath); ApplyBindingMode(binding, interpretedProperty); ApplyValidation(binding, interpretedViewModelType, interpretedProperty, foundControl); ApplyValueConverter(binding, boundProperty !, interpretedProperty); ApplyUpdateSourceTrigger(boundProperty !, foundControl, binding, interpretedProperty); ApplyStringFormat(binding, foundControl, interpretedProperty); BindingOperations.SetBinding(foundControl, boundProperty, binding); } //var foundControl = firstList.Single(); } }
//private CustomBasicList<FrameworkElement> GetBasic public static void Bind(object viewModel, DependencyObject view, DependencyObject?content = null) { Type type = viewModel.GetType(); if (!(view is FrameworkElement element)) { return; } var viewType = viewModel.GetType(); var properties = viewType.GetProperties(); var methods = viewType.GetMethods().ToCustomBasicList(); CustomBasicList <FrameworkElement> controls; controls = FindVisualChildren(view).ToCustomBasicList(); if (controls.Count == 0 && content != null) { controls = FindVisualChildren(content).ToCustomBasicList(); if (controls.Count == 0) { if (content is FrameworkElement cc) { controls = new CustomBasicList <FrameworkElement>() { cc } } } ; } controls.AddRange(ManuelElements); if (controls.Count == 0) { throw new BasicBlankException("There was no controls. Rethink"); } //if (!_boundProperties.TryGetValue(foundControl.GetType(), out boundProperty!)) // continue; BindProperties(controls, type, properties, (list, p) => { return(list.Where(x => x.Name.StartsWith(p.Name, StringComparison.InvariantCultureIgnoreCase)).ToCustomBasicList()); }, item => { bool rets = _boundProperties.TryGetValue(item, out DependencyProperty? d); BindHelpClass bind = new BindHelpClass(); bind.IsValid = rets; bind.Dependency = d; return(bind); }, item => { return(item.Name); } ); var pList = methods.Where(x => x.Name.StartsWith("Can")).ToCustomBasicList(); methods.KeepConditionalItems(x => x.HasCommandAttribute()); //because only if (methods.Any(x => { if (x.ReturnType.Name != "Task" && x.ReturnType.Name != "Void") { return(true); } return(false); })) { throw new BasicBlankException("Cannot put as command if the return type of any is not void or task. Rethink"); } BindStandardCommands(controls, viewModel, methods, properties, pList); }