2

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)));
Elisabeth
  • 20,496
  • 52
  • 200
  • 321

2 Answers2

1

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;
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
  • your code is fine BUT my Resource "GroupHeaderStyle" is defined in another XAML file. The code you gave me is inside a .cs custom control file. How would you access the ressource... ok I make an extra question of it thats better... – Elisabeth Nov 06 '10 at 11:39
  • Updated my answer, something similar to this depending on assembly and directory – Fredrik Hedblad Nov 06 '10 at 11:48
0

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.

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • updated my question with new code: How can I create a ContainerStyle because its NULL ? funny I can create a Style and assign it to ContainerStyle I thought it takes ContainerSTyle instances only... – Elisabeth Nov 06 '10 at 11:19