If I make all GridItems invisible, then the plus sign remains in RadPropertyGrid for Font item.
Example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Make();
}
private void Make()
{
radPropertyGrid1.SelectedObject = new Item();
foreach (var item in radPropertyGrid1.Items)
{
if (!(item.Value is Font)) continue;
foreach (var subItem in item.GridItems)
{
subItem.Visible = false;
}
}
}
public class Item
{
[DisplayName("Font"), Category("Font")]
public Font Id { get; set; }
public Item()
{
Id = new Font("Arial", 12, FontStyle.Regular);
}
}
}
7 Answers, 1 is accepted
You can use the ItemFormatting event to hide the expander item:
private
void
RadPropertyGrid1_ItemFormatting(
object
sender, Telerik.WinControls.UI.PropertyGridItemFormattingEventArgs e)
{
if
(e.Item.Name ==
"Id"
)
{
var el = e.VisualElement
as
PropertyGridItemElement;
el.ExpanderElement.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
}
}
I hope this will be useful. Let me know if you have additional questions.
Regards,
Dimitar
Progress Telerik

if I modify my procedure Make() and add
radPropertyGrid1.ExpandAllGridItems();
at the end, then when you change the font (or reset), all hidden topics reappear
Full text:
private void Make()
{
radPropertyGrid1.SelectedObject = new Item();
foreach (var item in radPropertyGrid1.Items)
{
if (!(item.Value is Font)) continue;
foreach (var subItem in item.GridItems)
{
subItem.Visible = false;
}
}
radPropertyGrid1.ExpandAllGridItems();
}
I need ExpandAllGridItems, because the object in the radPropertyGrid is actually very complex and contains many properties
You can use the ItemExpandedChanging event to prevent particular items from expanding (make sure to subscribe before calling ExpandAllGridItems):
public
RadForm1()
{
InitializeComponent();
radPropertyGrid1.ItemExpandedChanging += RadPropertyGrid1_ItemExpandedChanging;
Make();
}
private
void
RadPropertyGrid1_ItemExpandedChanging(
object
sender, RadPropertyGridCancelEventArgs e)
{
if
(e.Item.Name ==
"Id"
)
{
e.Cancel =
true
;
}
}
Should you have any other questions do not hesitate to ask.
Regards,
Dimitar
Progress Telerik

As you advised, I added two events for radpropertygrid:
private void radProperty_ItemExpandedChanging(object sender, RadPropertyGridCancelEventArgs e)
{
if (e.Item is PropertyGridItem item && item.Value is Font)
{
e.Cancel = true;
}
}
private void radProperty_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
{
if (e.Item is PropertyGridItem itemF && itemF.Value is Font)
{
if (e.VisualElement is PropertyGridItemElement el) el.ExpanderElement.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
}
}
Everything works correctly. But as soon as I start to scroll through the contents, there are visual artifacts.
In the beginning, I scroll down until the upper elements disappear. Then scroll up to see the top elements.
Notice where the "Size" and "Value" are now drawn.
If you scroll through the contents again, some of the elements are randomly and unpredictably drawn in the wrong place.
Since the elements are reused the Visibility should be reset in the ItemFormatting event handler:
private
void
RadPropertyGrid1_ItemFormatting(
object
sender, Telerik.WinControls.UI.PropertyGridItemFormattingEventArgs e)
{
if
(e.Item
is
PropertyGridItem itemF && itemF.Value
is
Font)
{
if
(e.VisualElement
is
PropertyGridItemElement el)
el.ExpanderElement.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
}
else
{
if
(e.VisualElement
is
PropertyGridItemElement el)
el.ExpanderElement.ResetValue(LightVisualElement.VisibilityProperty, Telerik.WinControls.ValueResetFlags.Local);
}
}
Another approach is to hide the expander item (the cross) only (it is synchronized internally, so no need to reset):
private
void
RadPropertyGrid1_ItemFormatting(
object
sender, Telerik.WinControls.UI.PropertyGridItemFormattingEventArgs e)
{
if
(e.Item
is
PropertyGridItem itemF && itemF.Value
is
Font)
{
if
(e.VisualElement
is
PropertyGridItemElement el)
el.ExpanderElement.ExpanderItem.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
}
}
Let me know how this works for you.
Regards,
Dimitar
Progress Telerik

The first approach still has an artifact: "font" is shifted to the left.
Second approach solved all the problems.
This is actually expected because the entire expander element is hidden. Nevertheless, I am glad that the second approach is working well for your case.
Do not hesitate to contact us if you have other questions.
Dimitar
Progress Telerik