// [TestMethod] // TODO public void BindCollection() { //PersonVM vm = null; PersonVMView view = new PersonVMView(); DataGrid grid = new DataGrid(); var nameColumn = new DataGridTextColumn(); var memberCountColumn = new DataGridTextColumn(); grid.Columns.Add(nameColumn); grid.Columns.Add(memberCountColumn); ViewBinder.BindVM(view, b => { b.Collection(x => x.Projects).To(grid, i => { i.Property(x => x.Name).To(nameColumn); i.Property(x => x.MemberCount).To(memberCountColumn); }); }); Binding itemsSourceBinding = BindingOperations.GetBinding(grid, DataGrid.ItemsSourceProperty); Assert.IsNotNull(itemsSourceBinding); Assert.IsNull(itemsSourceBinding.Source); Assert.AreEqual("Projects", itemsSourceBinding.Path.Path); Binding nameBinding = (Binding)nameColumn.Binding; Binding memberCountBinding = (Binding)memberCountColumn.Binding; Assert.AreEqual("Name", nameBinding.Path.Path); Assert.AreEqual("MemberCount", memberCountBinding.Path.Path); Assert.IsNull(nameBinding.Source); Assert.IsNull(memberCountBinding.Source); }
public void BindRootView() { var rootView = new RootView() { Contexts = new List <ReflectContext>() }; rootView.Contexts.Add(new ReflectContext() { Folder = "C:\teste", Name = "Context1" } ); rootView.Contexts.Add(new ReflectContext() { Folder = "C:\teste2", Name = "Context2" } ); ViewBinder.RootFolder = @"..\..\..\Views\ReflectorView.Test\"; var output = ViewBinder.BindView(rootView); var sb = new StringBuilder(); using (TextReader reader = File.OpenText(ViewBinder.RootFolder + "RootViewOut.txt")) { sb.Append(reader.ReadToEnd()); } Assert.AreEqual(sb.ToString(), output); }
void ViewBinderDisplay_Loaded(object sender, RoutedEventArgs e) { IViewBinder instance = ViewBinder.GetInstance(this.Parent); ViewBinder<ViewBinderViewModel> binder = new ViewBinder<ViewBinderViewModel>(this, new ViewBinderViewModel(instance)); //binder.List(i => i.Binders); binder.Bind(); }
public void When_binder_is_constructed_then_viewmodel_is_initialized() { UserControl view = new UserControl(); object viewModel = new object(); var binder = new ViewBinder(view, viewModel); Assert.AreSame(viewModel, binder.Model); }
public void When_method_binder_is_added_then_it_can_be_retrieved_by_its_event() { var button = new Button(); var view = new UserControl(); var vm = new MockViewModelWithAction(); var buttonBinder = new MethodBinder<RoutedEventArgs>(button, "Click", p => vm.Action()); var viewModelBinder = new ViewBinder<object>(view, vm); viewModelBinder.Add(buttonBinder); var binder = viewModelBinder.GetMethodBinder(button, "Click"); Assert.AreEqual(buttonBinder, binder); }
public void When_list_binder_is_added_then_it_can_be_retrieved_by_its_type() { ListBox citiesList = new ListBox(); CustomerListViewModel vm = new CustomerListViewModel("Jeff", "Glenn"); ListBinder<Customer,CustomerListViewModel> binder = new ListBinder<Customer,CustomerListViewModel>(citiesList, p => vm.Customers, p => vm.SelectedCustomer,vm); var viewModelBinder = new ViewBinder<CustomerListViewModel>(citiesList, vm); viewModelBinder.Add(binder); var binders = viewModelBinder.GetBinders<ListBinder<Customer,CustomerListViewModel>>(citiesList); Assert.AreEqual(binder, binders.Single(), "There should be a single list binder found"); }
public void SetValue(T Value) { bool success = false; if (ViewBinder != null && !String.IsNullOrWhiteSpace(PropertyName) && CanWrite) { success = ViewBinder.SetValue(PropertyName, Value); } if (!success) { RaiseBindFailEvent(BindAction.Set, PropertyName); } }
private static void ModelPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) { if (e.NewValue != null) { var view = ViewLocator.Instance.LocateForModel(e.NewValue, target); ViewBinder.Bind(view, e.NewValue); SetContentProperty(target, view); } else { SetContentProperty(target, e.NewValue); } }
public void BindContentArea(CustomArea customArea, object viewModel, ViewBinder binder) { if (customArea == null || viewModel == null || binder == null) { return; } var vmType = viewModel.GetType(); var vmProperties = binder.GetViewModelProperties(vmType); if (vmProperties == null) { return; } var propertyContentAreaBindings = vmProperties.Select(pi => new AttributeBinding <CustomAreaAttribute>(pi.GetCustomAttribute <CustomAreaAttribute>(), pi)).Where(a => a.Attr != null); // Property bindings always take precedence over class level bindings var propertyBinding = propertyContentAreaBindings.Where(a => a.Attr.ContentArea == customArea.ContentArea).FirstOrDefault(); if (propertyBinding != null) { customArea.ContentTemplate = binder.GetUntypedTemplate(propertyBinding.Attr.Template); customArea.Content = propertyBinding.Property.GetValue(viewModel, null); } else { // Since no properties matched this custom area we need to check the view model itself to see if it should be bound to the custom area var classContentAreaBindings = vmType.GetCustomAttributes(typeof(CustomAreaAttribute), false).OfType <CustomAreaAttribute>(); var contentArea = classContentAreaBindings.Where(caa => caa.ContentArea == customArea.ContentArea); if (contentArea == null) { return; } // The view model needs to be bound to the custom area so now lets figure out what template to use var customAreaAttr = vmType.GetCustomAttribute <CustomAreaAttribute>(); if (customAreaAttr == null) { return; } customArea.ContentTemplate = binder.GetUntypedTemplate(customAreaAttr.Template); customArea.Content = viewModel; } }
// [TestMethod] // TODO public void BindScreen() { PersonScreenView screenView = new PersonScreenView(); TextBox targetElement = new TextBox(); ViewBinder.BindScreen <PersonScreen>(screenView, c => { c.BindVM(x => x.VM, b => { b.Property(x => x.FirstName).To(targetElement).On(TextBox.TextProperty); }); }); Binding binding = BindingOperations.GetBinding(targetElement, TextBox.TextProperty); Assert.AreEqual("VM.FirstName", binding.Path.Path); Assert.IsNull(binding.Source); }
private void ProcessRequest(object contextobj) { var context = (HttpListenerContext)contextobj; try { if (context.Request.HttpMethod.Equals("GET")) { var msg = context.Request.HttpMethod + " " + context.Request.Url; var runner = Router.Route(context.Request.Url.LocalPath); var view = runner.Run(); var response = ViewBinder.BindView(view); var b = Encoding.UTF8.GetBytes(response); context.Response.ContentLength64 = b.Length; context.Response.OutputStream.Write(b, 0, b.Length); context.Response.StatusCode = 200; } else { throw new Exception(context.Request.HttpMethod + " Not Implemented"); } } catch (Exception ex) { //LOG ERRORS TO Console Console.WriteLine("Request error: " + ex); var exceptionView = new ExceptionView() { Exception = ex }; var response = ViewBinder.BindView(exceptionView); byte[] b = Encoding.UTF8.GetBytes(response); context.Response.ContentLength64 = b.Length; context.Response.OutputStream.Write(b, 0, b.Length); context.Response.StatusCode = 500; } finally { context.Response.Close(); } }
public void KeyGesture_IsAddedToInputBindingsOfTargetObject() { var target = new Button(); var view = new TestView(); ViewBinder.BindVM(view, b => { b.Property(x => x.ViewModelCommand).To(target).KeyGesture(Key.A, ModifierKeys.Control); }); Assert.AreEqual(1, target.InputBindings.Count); var inputBinding = (KeyBinding)target.InputBindings[0]; Assert.AreEqual(Key.A, inputBinding.Key); var binding = BindingOperations.GetBinding(inputBinding, KeyBinding.CommandProperty); Assert.AreEqual("ViewModelCommand", binding.Path.Path); }
public override bool TrySetMember(SetMemberBinder Binder, Object Value) { bool success = false; String memberName = Binder.Name == "Value" && !String.IsNullOrWhiteSpace(PropertyName) ? PropertyName : Binder.Name; if (ViewBinder != null && !String.IsNullOrWhiteSpace(memberName) && CanWrite) { success = ViewBinder.SetValue(memberName, Value); } if (!success) { RaiseBindFailEvent(BindAction.Set, memberName); } return(true); }
public override bool TryGetMember(GetMemberBinder Binder, out Object Result) { bool success = false; Result = null; String memberName = Binder.Name == "Value" && !String.IsNullOrWhiteSpace(PropertyName) ? PropertyName : Binder.Name; if (ViewBinder != null && !String.IsNullOrWhiteSpace(memberName) && CanRead) { if (OneTime) { success = true; Result = null; if (CanRead) { success = ViewBinder.GetValue(memberName, out value); } if (success) { Result = value; } } else { success = ViewBinder.GetValue(memberName, out Result); } } if (!success) { RaiseBindFailEvent(BindAction.Get, memberName); } return(true); }
public T GetValue() { T result = default(T); bool success = false; if (ViewBinder != null && !String.IsNullOrWhiteSpace(PropertyName) && CanRead) { if (OneTime) { success = true; if (CanRead) { success = ViewBinder.GetValue(PropertyName, out value); } if (success) { result = (T)value; } } else { Object value; success = ViewBinder.GetValue(PropertyName, out value); if (success) { result = (T)value; } } } if (!success) { RaiseBindFailEvent(BindAction.Get, PropertyName); } return(result); }
public void BindContentArea(ItemContainer container, object viewModel, ViewBinder binder) { if (container == null || viewModel == null || binder == null) { return; } var vmType = viewModel.GetType(); var vmProperties = binder.GetViewModelProperties(vmType); if (vmProperties == null) { return; } var itemsSources = vmProperties.Select(pi => new AttributeBinding <ItemsSourceAttribute>(pi.GetCustomAttribute <ItemsSourceAttribute>(), pi)).Where(a => a.Attr != null); var treeSources = vmProperties.Select(pi => new AttributeBinding <TreeSourceAttribute>(pi.GetCustomAttribute <TreeSourceAttribute>(), pi)).Where(a => a.Attr != null); BindItemSources(container, itemsSources, viewModel, binder); BindTreeViews(container, treeSources, viewModel, binder); }
private void BindTreeViews(ItemContainer container, IEnumerable <AttributeBinding <TreeSourceAttribute> > treesources, object viewModel, ViewBinder binder) { // We need to determine what property in the view model should be bound to this container // First check for any content area attributes on the marked up properties // Using the new attribute binding simplifies the linq in the next line var altBindings = treesources.Select(isa => new AttributeBinding <ContentAreaAttribute>(isa.Property.GetCustomAttribute <ContentAreaAttribute>(), isa.Property)); // Now check to see which of those has a content area that matches the name of this container var matchingBinding = altBindings.FirstOrDefault(ab => (string.IsNullOrWhiteSpace(container.ContentArea) && (ab.Attr == null || string.IsNullOrWhiteSpace(ab.Attr.ContentArea))) || (ab.Attr != null && container.ContentArea == ab.Attr.ContentArea)); // Switch back to dealing with the original attribute binding var treeSource = treesources.FirstOrDefault(isa => isa.Property == matchingBinding.Property); if (treeSource == null) { return; } var collection = treeSource.Property.GetValue(viewModel, null); if (collection == null) { return; } var control = binder.ResolveControlBinding(typeof(TreeSourceAttribute)); if (control == null) { return; } container.Content = control; control.CreateBinding("ItemsSource", treeSource.Property.Name); // Now we need to get the template type of teh objects we're going to put inside the tree view var templateType = collection.GetType().GetUnderlyingType(); var ptOverride = treeSource.Attr.ParentTemplateOverride; var parentTemplate = binder.GetDataTemplateBinding(templateType, ptOverride ?? null); var treeView = control as TreeView; if (treeView != null) { treeView.ItemTemplate = parentTemplate.Template; } /// TODO: Make the child template stuff work appropriately without having to directly reference the child template from the parent template in XAML /// see HierarchichalTemplates.xaml for the example }
public void BindContentArea(Toolbar toolbar, object viewModel, ViewBinder binder) { if (toolbar == null || viewModel == null || binder == null) { return; } var vmType = viewModel.GetType(); var vmProperties = binder.GetViewModelProperties(vmType); if (vmProperties == null) { return; } var toolbarActions = vmProperties.Select(pi => new AttributeBinding <ToolbarActionAttribute>(pi.GetCustomAttribute <ToolbarActionAttribute>(), pi)).Where(a => a.Attr != null); if (toolbarActions.IsNullOrEmpty()) { return; } var attrType = typeof(ToolbarActionAttribute); toolbarActions.ForEach(tba => { var contentAreaAttribute = tba.Property.GetCustomAttribute <ContentAreaAttribute>(); // For every toolbar action in this view model we need to check to see if it should be added to this toolbar if ((string.IsNullOrWhiteSpace(toolbar.ContentArea) && (contentAreaAttribute == null || string.IsNullOrWhiteSpace(contentAreaAttribute.ContentArea))) || (contentAreaAttribute != null && toolbar.ContentArea == contentAreaAttribute.ContentArea)) { var controlBinding = tba.Attr.ControlTypeOverride != null ? new ControlTypeBinding(tba.Attr.ControlTypeOverride) : binder.GetControlBinding(attrType); var control = controlBinding.ControlType.SafeCreate <FrameworkElement>(); if (controlBinding.IsCommandSource || controlBinding.HasCommandProperty) { control.CreateBinding("Command", tba.Property.Name); } var text = !string.IsNullOrWhiteSpace(tba.Attr.Name) ? tba.Attr.Name : tba.Property.Name.Replace("Command", string.Empty).SeparateOnCamelCase(); // Now not every button type in the world is going to use the content property to display the unique content inside the button. Check for a content mapper // to determine what property to set in order to add the actual content. If no mapper can be found, assume we're using the content property var contentProperty = "Content"; var mapper = binder.GetContentMapper(controlBinding.ControlType); if (mapper != null) { contentProperty = mapper.ContentProperty; } control.SetProperty(contentProperty, text); // Now check and see if there is a visibility attribute var vizAttr = tba.Property.GetCustomAttribute <VisibilityAttribute>(); if (vizAttr != null) { var converter = vizAttr.ConverterType.SafeCreate <IValueConverter>(); control.CreateBinding("Visibility", vizAttr.Path ?? tba.Property.Name, converter: converter, converterParameter: vizAttr.ConverterParameter); } // Now that we're all setup go ahead and add the control to the toolbar toolbar.Children.Add(control); } }); }
public void When_value_binder_is_added_then_it_can_be_retrieved_by_its_bound_property() { var view = new UserControl(); var viewModel = new { Value = "my value" }; var valueBinder = new ValueBinder<object>(view, UserControl.ContentProperty, p => viewModel.Value) {BindingMode = BindingMode.OneWay}; var viewModelBinder = new ViewBinder<object>(view, viewModel); viewModelBinder.Add(valueBinder); var binder = viewModelBinder.GetPropertyBinder(view, UserControl.ContentProperty); Assert.AreEqual(valueBinder, binder); }
public void When_value_binder_is_added_then_it_can_be_retrieved_by_its_type() { var view = new UserControl(); var mockBinder = new MockBinder(view); var viewModelBinder = new ViewBinder<object>(view, null); viewModelBinder.Add(mockBinder); var binders = viewModelBinder.GetBinders<MockBinder>(view); Assert.AreEqual(mockBinder, binders.Single(), "There should be a single binder found that equals our mockBinder"); }
public void When_retrieving_default_property_for_CheckBox_then_IsCheckedProperty_is_returned() { var checkBox = new CheckBox(); var binder = new ViewBinder(); var property = ViewBinder.GetPropertyForElement(checkBox); Assert.AreEqual(ToggleButton.IsCheckedProperty, property); }
private void BindItemSources(ItemContainer container, IEnumerable <AttributeBinding <ItemsSourceAttribute> > itemsources, object viewModel, ViewBinder binder) { // We need to determine what property in the view model should be bound to this container // First check for any content area attributes on the marked up properties // Using the new attribute binding simplifies the linq in the next line var altBindings = itemsources.Select(isa => new AttributeBinding <ContentAreaAttribute>(isa.Property.GetCustomAttribute <ContentAreaAttribute>(), isa.Property)); // Now check to see which of those has a content area that matches the name of this container var matchingBinding = altBindings.FirstOrDefault(ab => (string.IsNullOrWhiteSpace(container.ContentArea) && (ab.Attr == null || string.IsNullOrWhiteSpace(ab.Attr.ContentArea))) || (ab.Attr != null && container.ContentArea == ab.Attr.ContentArea)); // Switch back to dealing with the original attribute binding var itemsSource = itemsources.FirstOrDefault(isa => isa.Property == matchingBinding.Property); if (itemsSource == null) { return; } var collection = itemsSource.Property.GetValue(viewModel, null); if (collection == null) { return; } var control = binder.ResolveControlBinding(typeof(ItemsSourceAttribute)); if (control == null) { return; } container.Content = control; // Get the template type of the object we're going to put inside the item container var templateType = collection.GetType().GetUnderlyingType(); var template = itemsSource.Attr.Template; var dataTemplate = binder.GetDataTemplateBinding(templateType, template); if (dataTemplate != null) { (control as ItemsControl).ItemTemplate = dataTemplate.Template; } control.CreateBinding("ItemsSource", itemsSource.Property.Name); }