DataGrid CustomControl - How?

Dec 15, 2008 at 9:06 PM
Hi,

I am trying to create a custom DataGrid with several additional features. I want to create an assembly that contains this new grid that I can then include in different projects. I have created a class "myDataGrid" based on the WPF toolkit DataGrid as follows:

namespace WPFCustomControlLibrary
{
public partial class myDataGrid : DataGrid
...
}

This is OK so far, but I cannot seem to apply any styles to the original DataGrid. Styles created in Themes\generic.xaml have no effect and I cannot "find" them from the code behind. I realise this is kind of WPF generic, but it is related to the DataGrid as well!

Any help greatly appreciated.
Coordinator
Dec 16, 2008 at 1:15 AM
You need to override the DefaultStyleKeyProperty.  See the sample here, http://blogs.msdn.com/vinsibal/archive/2008/10/31/wpf-datagrid-frozen-row-sample.aspx, which derives from DataGrid and creates a different default style on the DataGrid.
Dec 16, 2008 at 8:00 PM
Thank you, Vinsibal. I have looked at this example and see where you have added the .OverrideMetadata to the constructor for the custom DataGrid. I want to override the DataGridColumnHeader style as well, I am guessing that I have to add something similar:

ColumnHeaderStyleProperty.OverrideMetadata(????).

Could you help me a bit further here, I have not been able to find any information on what to supply as arguments here (assuming I am remotely on the right lines). Hopefully one more example may help me work out the rest (DataGridRowHeader etc) for myself.

Thanks
Dec 18, 2008 at 7:20 PM
Hi,

I am still struggling with the CustomColumnHeaderStyle. I have tried a few things, such as:

 

Type colHeaderType = typeof(CustomDataGridColumnHeader);

 

DefaultStyleKeyProperty.OverrideMetadata(colHeaderType,

new FrameworkPropertyMetadata(colHeaderType));

but don't seem to be able to change the default header style. The datagrid bit is working OK.

 

Dec 29, 2008 at 10:39 AM
Hi,

I now have this all working to my satisfaction. I actually moved all my Styles to a different xaml file (ResourceDictionary) and loaded it in MyDataGrid : DataGrid constructor:

 

this.Resources.MergedDictionaries.Add(SharedDictionaryManager.SharedDictionary);

SharedDictionaryManager I found on blogs.MSDN.com.

There doesn't seem to be any need to use DefaultStyleKeyProperty.OverrideMetadata now, presumably because the shared dictionary resource is found before generic is searched.