Exemple #1
0
 public void TestConversion()
 {
     try
     {
         byte[]      data  = new byte[] { 1, 2, 3, 4, 5 };
         MapImage    tile  = new MapImage(MapImageType.Bmp, data);
         MapImageWPF image = new MapImageWPF(tile);
     }
     catch (Exception ex)
     {
         Assert.IsInstanceOfType(ex, typeof(InvalidDataException));
     }
 }
        private static FrameworkElement CreateValueControl(PropertyInfo property, object targetObject)
        {
            if (property == null)
            {
                return(null);
            }
            if (targetObject == null)
            {
                return(null);
            }

            Type type = property.PropertyType;

            if (type == null)
            {
                return(null);
            }
            object val = property.GetValue(targetObject, null);

            FrameworkElement result = new FrameworkElement();

            if (type.IsEnum)
            {
                ComboBox comboBox = new ComboBox();
                comboBox.ItemsSource = type.GetEnumValues();
                Binding itemBinding = new Binding(property.Name)
                {
                    Mode = BindingMode.TwoWay, Source = targetObject, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                };
                comboBox.SetBinding(ComboBox.SelectedItemProperty, itemBinding);
                result = comboBox;
            }
            else
            {
                if (type.Equals(typeof(MapImageWPF)) || type.IsSubclassOf(typeof(MapImageWPF)))
                {
                    ImageSource imageSource = (val as MapImageWPF).Image;
                    Image       image       = new Image();
                    image.Source = imageSource ?? new BitmapImage(new Uri("Images/no_image.gif", UriKind.Relative));
                    Viewbox box = new Viewbox();
                    box.Child = image;
                    box.HorizontalAlignment = HorizontalAlignment.Center;
                    box.VerticalAlignment   = VerticalAlignment.Center;
                    box.Width  = 100;
                    box.Height = 100;
                    image.MouseLeftButtonUp += (s, e) =>
                    {
                        var dialog = new Microsoft.Win32.OpenFileDialog()
                        {
                            CheckFileExists = true, CheckPathExists = true
                        };
                        if (dialog.ShowDialog() == true)
                        {
                            string fileName = dialog.FileName;
                            byte[] data;
                            using (var file = File.OpenRead(fileName))
                            {
                                data = new byte[file.Length];
                                file.Read(data, 0, data.Length);
                            }
                            MapImageWPF newImage = new MapImageWPF(MapImageType.Custom, data);
                            image.Source = newImage.Image ?? new BitmapImage(new Uri("Images/no_image.gif", UriKind.Relative));
                            property.SetValue(targetObject, newImage, null);
                        }
                    };
                    result = box;
                }
                else
                {
                    if (type.Equals(typeof(bool)))
                    {
                        CheckBox checkBox    = new CheckBox();
                        Binding  boolBinding = new Binding(property.Name)
                        {
                            Mode = BindingMode.TwoWay, Source = targetObject, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, ValidatesOnDataErrors = true
                        };
                        checkBox.SetBinding(CheckBox.IsCheckedProperty, boolBinding);
                        result = checkBox;
                    }
                    else
                    {
                        TextBox textBox     = new TextBox();
                        Binding textBinding = new Binding(property.Name)
                        {
                            Mode = BindingMode.TwoWay, Source = targetObject, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, ValidatesOnDataErrors = true
                        };
                        textBox.SetBinding(TextBox.TextProperty, textBinding);
                        result = textBox;
                    }
                }
            }

            if (result != null)
            {
                result.Margin = new Thickness(5);
            }

            return(result);
        }