Exemplo n.º 1
0
        public void InitGrid(NPCPrompt entity)
        {
            dynamic.Children.Clear();

            int i = 0;
            foreach (var prop in entity.GetType().GetProperties())
            {
                var attr = prop.GetCustomAttributes(typeof(EditableFieldAttribute), false);
                if (attr.Any())
                {
                    dynamic.RowDefinitions.Add(new RowDefinition());

                    var val = prop.GetValue(entity, null) ?? string.Empty;
                    var textBlock = new TextBlock() { Text = ((EditableFieldAttribute)attr[0]).Display };
                    UIElement elementToAdd = null;
                    dynamic.Children.Add(textBlock);
                    Grid.SetRow(textBlock, i);
                    Grid.SetColumn(textBlock, 0);

                    var type = prop.PropertyType;

                    if (type.Equals(typeof(bool)))
                    {
                        elementToAdd = new CheckBox() { IsChecked = (bool)val };
                    }
                    else
                    {
                        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
                        {
                            var builder = new StringBuilder();
                            var j = 0;
                            foreach (var v in (IEnumerable)val)
                            {
                                if (j > 0)
                                    builder.Append(";\n");
                                builder.Append(CreateString(v, type.GetGenericArguments()[0]));
                                j++;
                            }

                            elementToAdd = new TextBox()
                            {
                                AcceptsReturn = true,
                                TextWrapping = TextWrapping.Wrap,
                                Height = double.NaN,
                                Text = builder.ToString()
                            };
                        }
                        else
                        {
                            elementToAdd = new TextBox() { Text = CreateString(val,type) };
                        }
                    }

                    dynamic.Children.Add(elementToAdd);
                    Grid.SetRow(elementToAdd, i);
                    Grid.SetColumn(elementToAdd, 1);

                    i++;
                }
            }
        }
Exemplo n.º 2
0
        public void UpdatePrompt(NPCPrompt prompt)
        {
            int i = 0;
            foreach (var prop in prompt.GetType().GetProperties())
            {
                var attr = prop.GetCustomAttributes(typeof(EditableFieldAttribute), false);
                if (attr.Any())
                {
                    var element = dynamic.Children
                      .Cast<UIElement>()
                      .First(e => Grid.GetRow(e) == i && Grid.GetColumn(e) == 1);
                    var type = prop.PropertyType;

                    if (type.Equals(typeof(bool)))
                        prop.SetValue(prompt, ((CheckBox)element).IsChecked, null);
                    else
                    {
                        var text = ((TextBox)element).Text;
                        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
                        {
                            var vals = text
                                .Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
                                .Select(x => x.Trim()).ToList();

                            vals.Select(x => ParseVal(x, type.GetGenericArguments()[0]));
                            prop.SetValue(prompt, vals, null);
                        }
                        else
                        {
                            prop.SetValue(prompt, ParseVal(text, type), null);
                        }
                    }

                    i++;
                }
            }
            UpdatePromptInDB(prompt);
        }