Reputation: 133
I did not find it on the Internet
Help rewrite this code to C#
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander x:Name="exp" IsExpanded="True" >
<Expander.Header>
<TextBlock/>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
My code
An error in the code takes off.
Style GroupStyle = new Style() { TargetType = typeof(GroupItem) };
Setter setter = new Setter { Property = Control.TemplateProperty };
ControlTemplate template = new ControlTemplate { TargetType = typeof(GroupItem) };
var expander = new FrameworkElementFactory(typeof(Expander));
expander.SetValue(Expander.IsExpandedProperty, false);
expander.SetValue(Expander.HeaderProperty, new TextBlock { }); // Error: System.NotSupportedException
expander.SetValue(Expander.ContentProperty, new ItemsPresenter());
template.VisualTree = expander;
setter.Value = template;
GroupStyle.Setters.Add(setter);
Doing datagrid grouping
Upvotes: 0
Views: 175
Reputation: 6683
As @arconaut has said, first line should use typeof
. So first tree lines are:
Style GroupStyle = new Style() { TargetType = typeof(GroupItem) };
Setter setter = new Setter { Property = Control.TemplateProperty };
ControlTemplate template = new ControlTemplate { TargetType = typeof(GroupItem) };
Then I believe IsExpandedProperty
should be set to True
based on the above XAML.
expander.SetValue(Expander.IsExpandedProperty, true);
Finally you need to pass FrameworkElementFactory
as argument for SetValue()
in the last two lines.
expander.SetValue(Expander.HeaderProperty,
new FrameworkElementFactory(typeof(TextBlock)));
expander.SetValue(Expander.ContentProperty,
new FrameworkElementFactory(typeof(ItemsPresenter)));
Upvotes: 1
Reputation: 3285
Try replacing your first line with this:
Style GroupStyle = new Style() { TargetType = typeof(GroupItem) };
The x:Type
markup extension has a similar function to the typeof()
operator in C#
https://learn.microsoft.com/en-us/dotnet/desktop/xaml-services/xtype-markup-extension
Upvotes: 1