xceedsoftware/wpftoolkit

IsExpandingNonPrimitiveTypes

EdoardoNagliati opened this issue · 4 comments

Good morning, I am trying out the Plus version of the WPF Toolkit and I am interested in using the IsExpandingNonPrimitiveTypes setting,

<xctk:PropertyGrid Grid.Row="1" x:Name="_propertyGrid" Width="450" MaxHeight="650" Margin="10"
SelectedObject="{Binding}"
ShowDescriptionByTooltip="True"
IsExpandingNonPrimitiveTypes="True">
</xctk:PropertyGrid>
However, I would like the expansion to occur only for certain data types, not for all. How can I manage this?
Thank you.

Hi,

Instead of using the IsExpandingNonPrimitiveTypes, which will make all possible subItem expandable, you can set the
[ExpandableObject()] attribute over the object you want to see as expandable:

<xctk:PropertyGrid Grid.Row="1"
x:Name="_propertyGrid"
Width="450"
MaxHeight="650"
Margin="10"
SelectedObject="{Binding}"
ShowDescriptionByTooltip="True"
IsExpandingNonPrimitiveTypes="False">
</xctk:PropertyGrid>

this.DataContext = new Player() { Infos = new Infos() { FirstName = "Kyle", LastName = "Matheson" }, Infos2 = new Infos2() { ThirdName = "John", FourthName = "Mike" }, Position = "Center" };

public class Player
{
[ExpandableObject()] //Only Infos will be expandable
public Infos Infos { get; set; }

public Infos2 Infos2 { get; set; }

public string Position { get; set; }
}

public class Infos
{
public string FirstName { get; set; }
public string LastName { get; set; }
}

public class Infos2
{
public string ThirdName { get; set; }
public string FourthName { get; set; }
}

Thank's for your answer.

I've another problem: the function ExpandendProperty doesn't work.

In the PropGrid class, I have a property named 'Fondazione' with the attribute 'RefreshProperty.All'. When the refresh is triggered, all properties in the PropertyGrid are collapsed, even if they were not collapsed before. To resolve this issue and maintain the appearance of the PropertyGrid unchanged, I thought about raising an event (OnBeforeFondazioneChanged) before the refresh and saving the expanded properties in a List. Then, after the refresh, the function PropertyValueChanged is called, which should expand each property in the list using ExpandProperty, but this does not work. The list is correctly populated, and it seems that ExpandProperty is specifically what is not working. I made an attempt where instead of calling ExpandProperty I call ExpandAllProperties, and this expands all the properties.

public List ExpandedProp;
public MainWindow()
{
InitializeComponent();
var pGrid=new PropGrid();
pGrid.Fondazione=false;

....
pGrid.svincoloI=new SvincoloI();
pGrid.svincoloJ=new SvincoloJ();
pGrid.armaturaInf=new Armatura();
pGrid.armaturaSup=new Armatura();

pGrid.BeforeFondazioneChanged+=MyCustomEventHandler;

ExpandedProp=new List<PropertyItem>();

this.DataContext = pGrid;

}

private void MyCustomEventHandler(object sender, EventArgs e)
{
foreach (PropertyItem prop in _propertyGrid.Properties)
{
if (prop.IsExpanded==true)
{
ExpandedProp.Add(prop);
}

 }

}

private void _propertyGrid_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e)
{
foreach (PropertyItem prop in ExpandedProp)
{
_propertyGrid.ExpandProperty(prop.PropertyName.ToString());
}
// _propertyGrid.ExpandAllProperties();
ExpandedProp.Clear();
}

public class PropGrid
{
public event EventHandler BeforeFondazioneChanged;

 private  bool mFondazione;

[Category("Interazione terreno")]
[RefreshProperties(RefreshProperties.All)]
[ReadOnly(false)]
public bool Fondazione
{
get { return mFondazione; }
set
{

    OnBeforeFondazioneChanged();

    mFondazione = value;
    PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())["k"];
    ReadOnlyAttribute attribute = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
    var fieldToChange = attribute.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(a => a.Name.Contains("readonly", StringComparison.OrdinalIgnoreCase));

    fieldToChange.SetValue(attribute, mFondazione ==false);
}

}

....
}

EDIT: I find that works if instead of
_propertyGrid.ExpandProperty(prop.PropertyName)
I use
_propertyGrid.ExpandProperty(prop.DispalyName)

but this raise a Binding failure: IsMiscCategoryLabelHidden

Hi,
This binding failure is related to the RefreshProperties attribute.
While refreshing, a MultiDataTrigger is validating the grouping and checking for a condition on IsMiscCategoryLabelHidden of a parent PropertyGrid. But the refresh re-creates the groups, and the MultiDataTrigger becomes useless in this situation. Just don`t bother about this warning.

Thanks