Xceed PropetyGrid

This commit is contained in:
Miguel 2024-06-02 09:13:01 +02:00
parent 667cd18f5d
commit 47735ef00a
15 changed files with 341 additions and 21 deletions

View File

@ -15,6 +15,7 @@
<local:BrushToColorNameConverter x:Key="BrushToColorNameConverter"/> <local:BrushToColorNameConverter x:Key="BrushToColorNameConverter"/>
<local:VerticalPositionConverter x:Key="VerticalPositionConverter"/> <local:VerticalPositionConverter x:Key="VerticalPositionConverter"/>
<local:SumConverter x:Key="SumConverter" /> <local:SumConverter x:Key="SumConverter" />
<local:ColorToBrushConverter x:Key="ColorToBrushConverter"/>
<local:StringToBrushConverter x:Key="StringToBrushConverter"/> <local:StringToBrushConverter x:Key="StringToBrushConverter"/>
</Application.Resources> </Application.Resources>
</Application> </Application>

View File

@ -61,6 +61,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Aether.Physics2D" Version="2.1.0" /> <PackageReference Include="Aether.Physics2D" Version="2.1.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" /> <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.6.0" />
<PackageReference Include="LiveChartsCore.SkiaSharpView.WPF" Version="2.0.0-rc2" /> <PackageReference Include="LiveChartsCore.SkiaSharpView.WPF" Version="2.0.0-rc2" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" /> <PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

View File

@ -14,6 +14,7 @@ using CtrEditor.Simulacion;
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Reflection;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using Xceed.Wpf.Toolkit.PropertyGrid;
namespace CtrEditor namespace CtrEditor
@ -528,9 +529,9 @@ namespace CtrEditor
} }
// Se cargan los datos de cada UserControl en el StackPanel // Se cargan los datos de cada UserControl en el StackPanel
public void CargarPropiedadesosDatos(osBase selectedObject, StackPanel PanelEdicion, ResourceDictionary Resources) public void CargarPropiedadesosDatos(osBase selectedObject, PropertyGrid PanelEdicion, ResourceDictionary Resources)
{ {
UserControlFactory.CargarPropiedadesosDatos(selectedObject, PanelEdicion, Resources); UserControlFactory.CargarPropiedadesosDatos(selectedObject, PanelEdicion);
} }
private RelayCommand saveCommand; private RelayCommand saveCommand;

View File

@ -4,6 +4,9 @@
xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Siemens="clr-namespace:CtrEditor.Siemens" xmlns:Siemens="clr-namespace:CtrEditor.Siemens"
xmlns:local="clr-namespace:CtrEditor"
xmlns:uc="clr-namespace:CtrEditor.ObjetosSim.UserControls"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:ObjetosSim="clr-namespace:CtrEditor.ObjetosSim" x:Class="CtrEditor.MainWindow" xmlns:ObjetosSim="clr-namespace:CtrEditor.ObjetosSim" x:Class="CtrEditor.MainWindow"
Height="900" Width="1600" WindowState="Maximized" Height="900" Width="1600" WindowState="Maximized"
ResizeMode="CanResize" Title="{Binding directorioTrabajo}" Icon="/app2.png"> ResizeMode="CanResize" Title="{Binding directorioTrabajo}" Icon="/app2.png">
@ -161,13 +164,11 @@
VerticalAlignment="Center"/> VerticalAlignment="Center"/>
<!-- PanelEdicion --> <!-- PanelEdicion -->
<ScrollViewer Grid.Row="2" Margin="5" VerticalScrollBarVisibility="Auto"> <xctk:PropertyGrid Grid.Row="2" Margin="5" Name="PanelEdicion" AutoGenerateProperties="False">
<StackPanel x:Name="PanelEdicion"> </xctk:PropertyGrid>
<!-- Aquí puedes agregar los controles para editar propiedades -->
</StackPanel>
</ScrollViewer>
<ToolBarTray Grid.Row="3">
<ToolBarTray Grid.Row="3">
<ToolBar> <ToolBar>
<Button Command="{Binding TBEliminarUserControlCommand}" ToolTip="Eliminar Control"> <Button Command="{Binding TBEliminarUserControlCommand}" ToolTip="Eliminar Control">
<StackPanel> <StackPanel>

View File

@ -418,7 +418,7 @@ namespace CtrEditor
private void ListaOs_SelectionChanged(object sender, SelectionChangedEventArgs e) private void ListaOs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ {
PanelEdicion.Children.Clear(); // Limpiar el panel existente //PanelEdicion.Children.Clear(); // Limpiar el panel existente
if (e.AddedItems.Count > 0 && e.AddedItems[0] is osBase selectedObject) if (e.AddedItems.Count > 0 && e.AddedItems[0] is osBase selectedObject)
CargarPropiedadesosDatos(selectedObject); CargarPropiedadesosDatos(selectedObject);

View File

@ -22,7 +22,7 @@
VerticalAlignment="Top" VerticalAlignment="Top"
HorizontalAlignment="Left" HorizontalAlignment="Left"
Background="Gray"/> Background="Gray"/>
<Ellipse Fill="{Binding ColorButton_oculto}" <Ellipse Fill="{Binding ColorButton_oculto, Converter={StaticResource ColorToBrushConverter}}"
Stroke="Black" Stroke="Black"
StrokeThickness="2" StrokeThickness="2"
Width="{Binding Tamano, Converter={StaticResource MeterToPixelConverter}}" Width="{Binding Tamano, Converter={StaticResource MeterToPixelConverter}}"

View File

@ -28,17 +28,17 @@ namespace CtrEditor.ObjetosSim
[ObservableProperty] [ObservableProperty]
public bool tipo_NC; public bool tipo_NC;
[ObservableProperty]
private Brush color;
partial void OnColorChanged(Brush value) [ObservableProperty]
Color color;
partial void OnColorChanged(Color value)
{ {
OnEstadoChanged(Estado); OnEstadoChanged(Estado);
} }
[ObservableProperty] [ObservableProperty]
private Brush colorButton_oculto; private Color colorButton_oculto;
[ObservableProperty] [ObservableProperty]
public float tamano; public float tamano;
[ObservableProperty] [ObservableProperty]
@ -47,7 +47,7 @@ namespace CtrEditor.ObjetosSim
partial void OnEstadoChanged(bool value) partial void OnEstadoChanged(bool value)
{ {
if (value) if (value)
ColorButton_oculto = Brushes.LightGreen; ColorButton_oculto = Colors.LightGreen;
else else
ColorButton_oculto = Color; ColorButton_oculto = Color;
if (!Tipo_NC) if (!Tipo_NC)
@ -74,7 +74,7 @@ namespace CtrEditor.ObjetosSim
Estado = false; Estado = false;
Tag = "M50.0"; Tag = "M50.0";
// Set initial color // Set initial color
Color = Brushes.LightBlue; Color = Colors.LightBlue;
} }
public override void UpdateGeometryStart() public override void UpdateGeometryStart()

View File

@ -1,6 +1,7 @@
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CtrEditor.Siemens; using CtrEditor.Siemens;
using System.ComponentModel;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
@ -30,6 +31,8 @@ namespace CtrEditor.ObjetosSim
public float min_OUT_Scaled; public float min_OUT_Scaled;
[ObservableProperty] [ObservableProperty]
public float max_OUT_Scaled; public float max_OUT_Scaled;
[TypeConverter(typeof(FloatToStringConverter))]
[ObservableProperty] [ObservableProperty]
public float value; public float value;

View File

@ -10,6 +10,12 @@ using CtrEditor.ObjetosSim.UserControls;
using System.Collections; using System.Collections;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Shapes; using System.Windows.Shapes;
using Xceed.Wpf.Toolkit.PropertyGrid;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using Xceed.Wpf.Toolkit;
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;
namespace CtrEditor.ObjetosSim namespace CtrEditor.ObjetosSim
{ {
@ -77,9 +83,73 @@ namespace CtrEditor.ObjetosSim
} }
} }
public static void CargarPropiedadesosDatos(object selectedObject, PropertyGrid propertyGrid)
{
// Clear previous properties
propertyGrid.SelectedObject = null;
propertyGrid.PropertyDefinitions.Clear();
// Add properties dynamically based on attributes
var properties = TypeDescriptor.GetProperties(selectedObject)
.Cast<PropertyDescriptor>()
.Where(prop => !prop.Attributes.OfType<HiddenAttribute>().Any());
public static void CargarPropiedadesosDatos(Object selectedObject, StackPanel PanelEdicion, ResourceDictionary Resources) foreach (var property in properties)
{
var displayNameAttr = property.Attributes.OfType<DisplayNameAttribute>().FirstOrDefault();
var propertyDefinition = new PropertyDefinition
{
TargetProperties = new[] { property.Name }
};
if (!property.Name.EndsWith("_oculto"))
{
if (displayNameAttr != null)
{
propertyDefinition.DisplayName = displayNameAttr.DisplayName;
}
else
{
propertyDefinition.DisplayName = property.Name.Replace("_", " ");
}
propertyGrid.PropertyDefinitions.Add(propertyDefinition);
}
}
// Create the DataTemplate for float and double editors
var doubleFactory = new FrameworkElementFactory(typeof(DoubleUpDown));
doubleFactory.SetValue(DoubleUpDown.FormatStringProperty, "F3");
doubleFactory.SetValue(DoubleUpDown.IncrementProperty, 0.001);
doubleFactory.SetValue(DoubleUpDown.MaximumProperty, 200000.599);
doubleFactory.SetBinding(DoubleUpDown.ValueProperty, new Binding("Value")
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
var doubleTemplate = new DataTemplate { VisualTree = doubleFactory };
// Add editor definitions for float and double
var floatEditorDefinition = new EditorDefinition
{
EditorTemplate = doubleTemplate,
TargetType = typeof(float)
};
var doubleEditorDefinition = new EditorDefinition
{
EditorTemplate = doubleTemplate,
TargetType = typeof(double)
};
propertyGrid.EditorDefinitions.Add(floatEditorDefinition);
propertyGrid.EditorDefinitions.Add(doubleEditorDefinition);
propertyGrid.SelectedObject = selectedObject;
}
public static void CargarPropiedadesosDatosex(Object selectedObject, StackPanel PanelEdicion, ResourceDictionary Resources)
{ {
PanelEdicion.Children.Clear(); PanelEdicion.Children.Clear();
var properties = selectedObject.GetType().GetProperties().OrderBy(p => p.Name); // Ordenar alfabéticamente var properties = selectedObject.GetType().GetProperties().OrderBy(p => p.Name); // Ordenar alfabéticamente

View File

@ -0,0 +1,29 @@
<UserControl x:Class="CtrEditor.ObjetosSim.UserControls.BrushEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CtrEditor.ObjetosSim.UserControls"
x:Name="brushEditorControl">
<ComboBox x:Name="colorComboBox"
SelectedValuePath="Color"
MinWidth="200"
SelectedValue="{Binding Path=Value, ElementName=brushEditorControl, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Width="16" Height="16" Fill="{Binding Color}" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemsSource>
<x:Array Type="{x:Type local:ColorItem}">
<local:ColorItem Name="Rojo" Color="{x:Static Brushes.Red}" />
<local:ColorItem Name="Azul" Color="{x:Static Brushes.Blue}" />
<local:ColorItem Name="Negro" Color="{x:Static Brushes.Black}" />
<local:ColorItem Name="Verde" Color="{x:Static Brushes.Green}" />
<local:ColorItem Name="Gris" Color="{x:Static Brushes.Gray}" />
</x:Array>
</ComboBox.ItemsSource>
</ComboBox>
</UserControl>

View File

@ -0,0 +1,33 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Xceed.Wpf.Toolkit.PropertyGrid;
namespace CtrEditor.ObjetosSim.UserControls
{
/// <summary>
/// Interaction logic for BrushEditor.xaml
/// </summary>
public partial class BrushEditor : UserControl
{
public BrushEditor()
{
InitializeComponent();
}
public Brush Value
{
get { return (Brush)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(Brush), typeof(BrushEditor), new PropertyMetadata(Brushes.Red));
}
public class ColorItem
{
public string Name { get; set; }
public Brush Color { get; set; }
}
}

View File

@ -12,6 +12,7 @@ using System.Windows.Media.Animation;
using System.Diagnostics; using System.Diagnostics;
using System.Windows.Shapes; using System.Windows.Shapes;
using System.Windows.Controls; using System.Windows.Controls;
using System.ComponentModel;
namespace CtrEditor.ObjetosSim namespace CtrEditor.ObjetosSim
{ {
@ -53,7 +54,7 @@ namespace CtrEditor.ObjetosSim
} }
} }
[TypeConverter(typeof(DoubleToStringConverter))]
public abstract partial class osBase : ObservableObject public abstract partial class osBase : ObservableObject
{ {
public virtual string Nombre { get; set; } = "osBase"; public virtual string Nombre { get; set; } = "osBase";

View File

@ -0,0 +1,14 @@
<Page x:Class="CtrEditor.PropiedadesExtendidas"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:CtrEditor"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="PropiedadesExtendidas">
<Grid>
<xctk:PropertyGrid Name="propertyGrid" />
</Grid>
</Page>

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CtrEditor
{
/// <summary>
/// Interaction logic for PropiedadesExtendidas.xaml
/// </summary>
public partial class PropiedadesExtendidas : Page
{
public PropiedadesExtendidas()
{
InitializeComponent();
}
}
}

View File

@ -2,9 +2,147 @@
using System.Windows.Data; using System.Windows.Data;
using System.Windows.Media; using System.Windows.Media;
using System.Windows; using System.Windows;
using System.ComponentModel;
using System.Reflection;
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;
using Xceed.Wpf.Toolkit;
using Xceed.Wpf.Toolkit.PropertyGrid;
namespace CtrEditor namespace CtrEditor
{ {
public class DecimalEditor : ITypeEditor
{
public FrameworkElement ResolveEditor(PropertyItem propertyItem)
{
var doubleUpDown = new DoubleUpDown
{
FormatString = "F3",
Increment = 0.001,
Maximum = 200000.599,
MinWidth = 100
};
var binding = new Binding("Value")
{
Source = propertyItem,
Converter = new DecimalConverter(),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
BindingOperations.SetBinding(doubleUpDown, DoubleUpDown.ValueProperty, binding);
return doubleUpDown;
}
}
public class DecimalConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double doubleValue)
{
return doubleValue.ToString("0.00", culture); // Formatear a dos decimales
}
if (value is float floatValue)
{
return floatValue.ToString("0.00", culture); // Formatear a dos decimales
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (double.TryParse(value as string, NumberStyles.Any, CultureInfo.InvariantCulture, out double doubleValue))
{
return doubleValue;
}
if (float.TryParse(value as string, NumberStyles.Any, CultureInfo.InvariantCulture, out float floatValue))
{
return floatValue;
}
return value;
}
}
public class ColorToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Color color)
{
return new SolidColorBrush(color);
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is SolidColorBrush brush)
{
return brush.Color;
}
return Binding.DoNothing;
}
}
[AttributeUsage(AttributeTargets.Property)]
public class HiddenAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Property)]
public class DisplayNameAttribute : Attribute
{
public string DisplayName { get; }
public DisplayNameAttribute(string displayName)
{
DisplayName = displayName;
}
}
public class DoubleToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double doubleValue)
{
return doubleValue.ToString("0.00", culture);
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string stringValue && double.TryParse(stringValue, NumberStyles.Float, culture, out double result))
{
return result;
}
return value;
}
}
public class FloatToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is float doubleValue)
{
return doubleValue.ToString("0.00", culture);
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string stringValue && float.TryParse(stringValue, NumberStyles.Float, culture, out float result))
{
return result;
}
return value;
}
}
public class StringToBrushConverter : IValueConverter public class StringToBrushConverter : IValueConverter
{ {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) public object Convert(object value, Type targetType, object parameter, CultureInfo culture)