将DataGrid GroupStyle XAML重写为C#代码

将DataGrid GroupStyle XAML重写为C#代码,c#,wpf,xaml,datagrid,groupstyle,C#,Wpf,Xaml,Datagrid,Groupstyle,有人能把这个XAML重写成C代码吗 无法使最后一行正常工作 更新: // Setup Grouping GroupStyle groupStyle = new GroupStyle(); groupStyle.ContainerStyle.Resources.FindName("GroupHeaderStyle"); groupStyle.Panel = new ItemsPanelTemplate(new Frame

有人能把这个XAML重写成C代码吗

无法使最后一行正常工作

更新:

 // Setup Grouping
            GroupStyle groupStyle = new GroupStyle();  
            groupStyle.ContainerStyle.Resources.FindName("GroupHeaderStyle");
            groupStyle.Panel = new ItemsPanelTemplate(new FrameworkElementFactory(typeof(DataGridRowsPresenter)));

此链接必须有帮助:

顺便说一句,你可能想要

groupStyle.ContainerStyle = Resources.FindName("GroupHeaderStyle");
而不是

groupStyle.ContainerStyle.Resources.FindName("GroupHeaderStyle");
编辑:
为了获得正确的容器,您需要从资源中获取它。这些是窗口资源或应用程序范围的资源。我猜
Application.Current.Resources.FindName(“GroupHeaderStyle”)
应该找到正确的资源,除非您正在做一些特殊的事情。

应该这样做:)


用新代码更新了我的问题:如何创建一个ContainerStyle,因为它是空的?有趣的是,我可以创建一个样式并将其分配给ContainerStyle我以为它只接受ContainerStyle实例…看到我在回答时更新了您的问题,您的代码正常了吗?但我的资源“GroupHeaderStyle”是在另一个XAML文件中定义的。你给我的代码在一个.cs自定义控件文件中。您将如何访问ressource。。。好的,我提出一个额外的问题,它更好…更新了我的答案,类似的东西,这取决于汇编和目录
groupStyle.ContainerStyle = Resources.FindName("GroupHeaderStyle");
groupStyle.ContainerStyle.Resources.FindName("GroupHeaderStyle");
FrameworkElementFactory datagridRowsPresenter = new FrameworkElementFactory(typeof(DataGridRowsPresenter));
ItemsPanelTemplate itemsPanelTemplate = new ItemsPanelTemplate();
itemsPanelTemplate.VisualTree = datagridRowsPresenter;
GroupStyle groupStyle = new GroupStyle();
groupStyle.Panel = itemsPanelTemplate;
dataGrid.GroupStyle.Add(groupStyle);

Uri resourceLocater = new Uri("/YourAssemblyName;component/SubDirectory/YourFile.xaml", System.UriKind.Relative);
ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
groupStyle.ContainerStyle = resourceDictionary["GroupHeaderStyle"] as Style;