将 DataGrid-GroupStyle XAML 重写为 C# 代码

发布于 2024-09-30 21:38:20 字数 1075 浏览 4 评论 0原文

有人可以将此 XAML 重写为 C# 代码吗?

<DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
 </DataGrid.GroupStyle>

我尝试了这个,但它不起作用:

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

无法让最后一行工作...

更新:

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

Can someone please rewrite this XAML into C# code ?

<DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
 </DataGrid.GroupStyle>

I tried this but it did not work:

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

Can`t get the last Line working...

UPDATE:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天邊彩虹 2024-10-07 21:38:20

这应该可以做到:)

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;

This should do it :)

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;
明月松间行 2024-10-07 21:38:20

此链接一定有帮助: http://www.netframeworkdev.com/windows-presentation-foundation-wpf/setting-an-itemscontrolpanels-content-from-code-86898.shtml

顺便说一句,您可能想要

groupStyle.ContainerStyle = Resources.FindName("GroupHeaderStyle");

而不是

groupStyle.ContainerStyle.Resources.FindName("GroupHeaderStyle");

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

This link must be helpful: http://www.netframeworkdev.com/windows-presentation-foundation-wpf/setting-an-itemscontrolpanels-content-from-code-86898.shtml

BTW, you perhaps want

groupStyle.ContainerStyle = Resources.FindName("GroupHeaderStyle");

instead of

groupStyle.ContainerStyle.Resources.FindName("GroupHeaderStyle");

Edit:
in order to get the container correct, you need to get it from the resources. These are either Window resources, or the application-wide resources. I guess Application.Current.Resources.FindName("GroupHeaderStyle"); should find the correct resources, unless you are doing something special.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文