I have a working attached property defined like below
public static class UiExt
{
public static readonly DependencyProperty UiDataProperty = DependencyProperty.RegisterAttached(
"UiData",
typeof(UiControlData),
typeof(UiExt),
new PropertyMetadata(null));
[System.ComponentModel.Description("Connection informations")]
[AttachedPropertyBrowsableForType(typeof(UIElement))]
public static UiControlData GetUiData(DependencyObject d)
{
return (UiControlData)d.GetValue(UiDataProperty);
}
public static void SetUiData(DependencyObject d, UiControlData value)
{
d.SetValue(UiDataProperty, value);
}
}
The propertie is of type UiControlData which is an object
public class UiControlData
{
public UiControlData()
{
}
public string Address { get; set; }
/// <summary>
/// Id of the connector as declared in the connector table
/// </summary>
public string ConnectorId { get; set; }
public string VariableId { get; set; } = string.Empty;
public bool IsNotificationEnabled { get; set; } = true;
public bool IsOneWay { get; set; } = false; // for now only for comboboxes -> notification will be disabled for items and values but still there for enable / visible, will do a synchro read at init
}
When I am in Visual Studio Designer I am able to create the object from the property editor like this with the New button
and then if I select the property in the XAML I have the correct sub properties
I want to have more control on what is displayed, for example hide some sub properties depending on the type of the UiElement so I created a TypeConverter and when I set it as attribute of my object with ([TypeConverter(typeof(MyComplexObjectConverter))]) the New button from the Visual Studio Designer disappear. I have just set a simple TypeConverter to give a minimal example and the problem is the same
public class MyComplexObjectConverter : ExpandableObjectConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string) || sourceType == typeof(UiControlData))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value == null)
{
return null;
}
if (value is string)
{
return new UiControlData();
}
if (value is UiControlData)
{
return value;
}
return base.ConvertFrom(context, culture, value);
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection originalProperties = base.GetProperties(context, value, attributes);
return originalProperties;
}
}
I tried to debug the designer by attaching another Visual Studio to WpfSurface and it looks like the convert functions are not called. So I must be missing something.
I also tried to add some more attributes to the Get function of the attached property like this one [System.ComponentModel.TypeConverter(typeof(MyComplexObjectConverter))] but the behaviour did not change.
What can I try / test more ?