Пример #1
0
        public void Link(object viewElement, IEnumerable <LinkData> linkData, Action <object, object, string> createLinkAction)
        {
            if (!(viewElement is ItemsControl itemsControl))
            {
                return;
            }

            foreach (LinkData data in linkData)
            {
                PropertyInfo pInfo = data.ContextMemberInfo as PropertyInfo;

                switch (data.ViewElementName)
                {
                case nameof(ItemsControl.ItemsSource):
                    if (pInfo == null)
                    {
                        throw new NotSupportedException($"The {nameof(ItemsControl.ItemsSource)} must be linked to a property!");
                    }

                    if (!(typeof(IEnumerable).IsAssignableFrom(pInfo.PropertyType)))
                    {
                        throw new NotSupportedException($"The {nameof(ItemsControl.ItemsSource)} must be linked to a implementation of '{typeof(IEnumerable)}'!");
                    }

                    _linkHelper = new ItemsControlLinkHelper(itemsControl, createLinkAction);

                    BindingHandler.SetBinding(itemsControl, ItemsControl.ItemsSourceProperty, pInfo, data.Context, data.PropertyPath);
                    break;

                default:
                    throw new NotSupportedException($"The property name '{data.ViewElementName}' is not supported by '{GetType().Name}'!");
                }
            }
        }
Пример #2
0
        public void Link(object viewElement, IEnumerable <LinkData> linkData, Action <object, object, string> createLinkAction)
        {
            if (!(viewElement is TextBox textBox))
            {
                return;
            }

            _textBox = textBox;

            foreach (LinkData data in linkData)
            {
                PropertyInfo pInfo = data.ContextMemberInfo as PropertyInfo;

                switch (data.ViewElementName)
                {
                case nameof(TextBox.Text):
                    BindingHandler.SetBinding(textBox, TextBox.TextProperty, pInfo, data.Context);
                    break;

                case nameof(TextBox.IsEnabled):
                    SetIsEnabledState(data.Context, pInfo);

                    if (data.Context is INotifyPropertyChanged propertyChangedContext)
                    {
                        _propertyChangedHandler.AddNotifyPropertyChangedItem(propertyChangedContext, data.ContextMemberInfo.Name,
                                                                             () => SetIsEnabledState(data.Context, pInfo));
                    }
                    break;

                default:
                    throw new NotSupportedException($"The link for '{data.ViewElementName}' is not supported by '{GetType().Name}'!");
                }
            }
        }
Пример #3
0
        public void Link(object viewElement, IEnumerable <LinkData> linkData, Action <object, object, string> createLinkAction)
        {
            if (!(viewElement is Button button))
            {
                return;
            }

            _button = button;

            foreach (LinkData data in linkData)
            {
                PropertyInfo pInfo = data.ContextMemberInfo as PropertyInfo;

                switch (data.ViewElementName)
                {
                case nameof(Button.IsEnabled):
                    if (pInfo == null)
                    {
                        continue;
                    }

                    if (pInfo.PropertyType != typeof(bool))
                    {
                        throw new NotSupportedException($"The type '{pInfo.PropertyType}' is not supported for '{nameof(Button.IsEnabled)}'!");
                    }

                    button.IsEnabled = (bool)pInfo.GetValue(data.Context);

                    if (data.Context is INotifyPropertyChanged propertyChangedContext)
                    {
                        _propertyChangedHandler.AddNotifyPropertyChangedItem(propertyChangedContext, pInfo.Name,
                                                                             () => _button.IsEnabled = (bool)pInfo.GetValue(data.Context));
                    }
                    break;

                case nameof(Button.Click):
                    if (!(data.ContextMemberInfo is MethodInfo methodInfo))
                    {
                        continue;
                    }

                    _clickViewModelMethodInfo = methodInfo;
                    _clickContext             = data.Context;
                    _button.Click            += Button_Click;
                    break;

                case nameof(Button.Command):
                    if (pInfo == null || !typeof(ICommand).IsAssignableFrom(pInfo.PropertyType))
                    {
                        continue;
                    }

                    BindingHandler.SetBinding(button, ButtonBase.CommandProperty, pInfo, data.Context);
                    break;

                default:
                    throw new NotSupportedException($"The link for '{data.ViewElementName}' is not supported by '{GetType().Name}'!");
                }
            }
        }
Пример #4
0
        public void Link(object viewElement, IEnumerable <LinkData> linkData, Action <object, object, string> createLinkAction)
        {
            if (!(viewElement is DataGrid dataGrid))
            {
                return;
            }

            foreach (LinkData data in linkData)
            {
                PropertyInfo pInfo = data.ContextMemberInfo as PropertyInfo;

                switch (data.ViewElementName)
                {
                case nameof(DataGrid.ItemsSource):
                    if (pInfo == null || !typeof(IEnumerable).IsAssignableFrom(pInfo.PropertyType))
                    {
                        throw new NotSupportedException($"The member info for the '{nameof(DataGrid.ItemsSource)}' link is not of type '{typeof(PropertyInfo)}' or the property is not of type '{typeof(IEnumerable)}'!");
                    }

                    foreach (DataGridColumn col in dataGrid.Columns)
                    {
                        createLinkAction.Invoke(col, data.Context, col.GetValue(FrameworkElement.NameProperty) as string);
                    }

                    BindingHandler.SetBinding(dataGrid, ItemsControl.ItemsSourceProperty, pInfo, data.Context);
                    break;

                case nameof(DataGrid.SelectedItem):
                    if (pInfo == null || typeof(IEnumerable).IsAssignableFrom(pInfo.PropertyType))
                    {
                        throw new NotSupportedException($"The member info for the '{nameof(DataGrid.SelectedItem)}' link is not of type '{typeof(PropertyInfo)}' or the property is of type '{typeof(IEnumerable)}'!");
                    }

                    BindingHandler.SetBinding(dataGrid, Selector.SelectedItemProperty, pInfo, data.Context);
                    break;

                default:
                    throw new NotSupportedException($"The property name '{data.ViewElementName}' is not supported by '{GetType().Name}'!");
                }
            }
        }
Пример #5
0
        public void Link(object viewElement, IEnumerable <LinkData> linkData, Action <object, object, string> createLinkAction)
        {
            if (!(viewElement is ListView listView))
            {
                return;
            }

            foreach (LinkData data in linkData)
            {
                PropertyInfo pInfo = data.ContextMemberInfo as PropertyInfo;

                switch (data.ViewElementName)
                {
                case nameof(ListView.ItemsSource):
                    BindingHandler.SetBinding(listView, ItemsControl.ItemsSourceProperty, pInfo, data.Context);

                    if (listView.View is GridView view)
                    {
                        //get the window to which the list view belongs to retrieve the information of the columns (to get the name)
                        //because gridviewcolumn is no framework element therefore retrieving the name of a column with .GetValue() does not work
                        //the x:Name creates fields with the columns and the specified name in the window. Therefore the window is needed to
                        //to get the name of the columns with reflection.
                        Window window = Window.GetWindow(listView);

                        foreach (GridViewColumn col in view.Columns)
                        {
                            createLinkAction(col, data.Context, GetColumnName(col, window));
                        }
                    }
                    break;

                case nameof(ListView.SelectedItem):
                    BindingHandler.SetBinding(listView, Selector.SelectedItemProperty, pInfo, data.Context);
                    break;

                default:
                    throw new NotSupportedException($"Property '{data.ViewElementName}' is not supported by {GetType().Name}!");
                }
            }
        }
Пример #6
0
        public void Link(object viewElement, IEnumerable <LinkData> linkData, Action <object, object, string> createLinkAction)
        {
            if (!(viewElement is Label label))
            {
                return;
            }

            foreach (LinkData data in linkData)
            {
                PropertyInfo pInfo = data.ContextMemberInfo as PropertyInfo;

                switch (data.ViewElementName)
                {
                case nameof(Label.Content):
                    BindingHandler.SetBinding(label, ContentControl.ContentProperty, pInfo, data.Context);
                    break;

                default:
                    throw new NotSupportedException($"Property '{data.ViewElementName}' is not supported by {GetType().Name}!");
                }
            }
        }
Пример #7
0
        public void Link(object viewElement, IEnumerable <LinkData> linkData, Action <object, object, string> createLinkAction)
        {
            if (!(viewElement is UserControl userControl))
            {
                return;
            }

            _userControl = userControl;

            foreach (LinkData data in linkData)
            {
                PropertyInfo pInfo = data.ContextMemberInfo as PropertyInfo;

                switch (data.ViewElementName)
                {
                case nameof(UserControl.DataContext):
                    BindingHandler.SetBinding(userControl, FrameworkElement.DataContextProperty, pInfo, data.Context);
                    break;

                case nameof(UserControl.Visibility):
                    if (pInfo == null)
                    {
                        continue;
                    }

                    SetVisibilityState(pInfo, data.Context);

                    if (data.Context is INotifyPropertyChanged propertyChangedContext)
                    {
                        _propertyChangedHandler.AddNotifyPropertyChangedItem(propertyChangedContext, pInfo.Name,
                                                                             () => SetVisibilityState(pInfo, data.Context));
                    }
                    break;

                default:
                    throw new NotSupportedException($"The link for '{data.ViewElementName}' is not supported by '{GetType().Name}'!");
                }
            }
        }
Пример #8
0
        public void Link(object viewElement, IEnumerable <LinkData> linkData, Action <object, object, string> createLinkAction)
        {
            if (!(viewElement is ComboBox comboBox))
            {
                return;
            }

            foreach (LinkData data in linkData)
            {
                PropertyInfo pInfo = data.ContextMemberInfo as PropertyInfo;

                switch (data.ViewElementName)
                {
                case nameof(ComboBox.ItemsSource):
                    if (pInfo == null)
                    {
                        continue;
                    }

                    BindingHandler.SetBinding(comboBox, ItemsControl.ItemsSourceProperty, pInfo, data.Context);
                    break;

                case nameof(ComboBox.SelectedItem):
                    if (pInfo == null)
                    {
                        continue;
                    }

                    BindingHandler.SetBinding(comboBox, Selector.SelectedValueProperty, pInfo, data.Context);
                    break;

                default:
                    throw new NotSupportedException($"The link for '{data.ViewElementName}' is not supported by '{GetType().Name}'!");
                }
            }
        }