que utiliza el ejemplo excelente y muy claro por Kent Boogart como base para mis tipos personalizados.
Tengo algunos pequeños cambios que creo que deberían hacerse al programa de ejemplo para aclarar la relación entre CustomTypeDescriptor
y PropertyDescriptor
.
- Creo que los datos deben almacenarse en la instancia del objeto de tipo, no en los descriptores de propiedad.
- Por lo general, esperaría que cada instancia de tipo personalizado conservara su propia colección de descriptores de propiedades, en lugar de que sea estática. Para aclarar esto, he agregado más información (a
Type
) para escribir el descriptor de propiedad.
El segundo punto es realmente un problema de dominio, pero esperaría que un uso más típico requiriera datos de propiedad de instancia, ya que uno usa ese tipo cuando las propiedades no se conocen en tiempo de compilación.
MainWindow.xaml
<Window
x:Class="CTDExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock>Name:</TextBlock>
<TextBox Grid.Column="1" Text="{Binding Name}"/>
<TextBlock Grid.Row="1">Age:</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Age}"/>
<TextBlock Grid.Row="2" Grid.ColumnSpan="2">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} is {1} years old.">
<Binding Path="Name"/>
<Binding Path="Age"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
namespace CTDExample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var ctd = new MyCustomType();
ctd.AddProperty("Name", typeof(string)); // Now takes a Type argument.
ctd.AddProperty("Age", typeof(int));
DataContext = ctd;
}
}
}
MyCustomType.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace CTDExample
{
public class MyCustomType : CustomTypeDescriptor
{
// This is instance data.
private readonly ICollection<PropertyDescriptor> _propertyDescriptors = new List<PropertyDescriptor>();
// The data is stored on the type instance.
private readonly IDictionary<string, object> _propertyValues = new Dictionary<string, object>();
// The property descriptor now takes an extra argument.
public void AddProperty(string name, Type type)
{
_propertyDescriptors.Add(new MyPropertyDescriptor(name, type));
}
public override PropertyDescriptorCollection GetProperties()
{
return new PropertyDescriptorCollection(_propertyDescriptors.ToArray());
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetProperties();
}
public override EventDescriptorCollection GetEvents()
{
return null;
}
public override EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return null;
}
private class MyPropertyDescriptor : PropertyDescriptor
{
// This data is here to indicate that different instances of the type
// object may have properties of the same name, but with different
// characteristics.
private readonly Type _type;
public MyPropertyDescriptor(string name, Type type)
: base(name, null)
{
_type = type;
}
public override bool CanResetValue(object component)
{
throw new NotImplementedException();
}
public override Type ComponentType
{
get { throw new NotImplementedException(); }
}
public override object GetValue(object component)
{
MyCustomType obj = (MyCustomType)component;
object value = null;
obj._propertyValues.TryGetValue(Name, out value);
return value;
}
public override bool IsReadOnly
{
get { return false; }
}
public override Type PropertyType
{
get { return _type; }
}
public override void ResetValue(object component)
{
throw new NotImplementedException();
}
public override void SetValue(object component, object value)
{
var oldValue = GetValue(component);
if (oldValue != value)
{
MyCustomType obj = (MyCustomType)component;
obj._propertyValues[Name] = value;
OnValueChanged(component, new PropertyChangedEventArgs(Name));
}
}
public override bool ShouldSerializeValue(object component)
{
throw new NotImplementedException();
}
public override void AddValueChanged(object component, EventHandler handler)
{
// set a breakpoint here to see WPF attaching a value changed handler
base.AddValueChanged(component, handler);
}
}
}
}
Espero no haber hecho ningún aullador, ¡ya que esta es mi primera publicación!