nosirrahcd
nosirrahcd

Reputation: 1467

Silverlight: Style for Custom Control in Same Project

I have a Silverlight application with a custom control MyDataGrid which is a DataGrid with some extra features.

This is part of the project in which it is used. It also requires a custom style. I can add the style to my style resource dictionary and set TargetType="sdk:DataGrid" which works.

However, my style relies on a property MyProperty specific to MyDataGrid, so a warning appears in the editor. More importantly, the styles cannot be previewed in the designer.

They DO work when the code is run, however.

My question is: Is there a way to pull in local controls to a resource dictionary, rather than creating a separate project, compiling it to a .dll, and pulling in that assembly?

Thanks!

Upvotes: 1

Views: 422

Answers (1)

Mike Post
Mike Post

Reputation: 6460

It sounds like you made a custom control, but you haven't defined a default style for it. Try the following:

  1. In your project, add a folder at the root level called Themes.
  2. In the Themes folder, add a ResourceDictionary named generic.xaml.
  3. Add all of your MyDataGrid styles to generic.xaml.
  4. Make sure one of the styles is an implicit style (using BasedOn is a great solution here).
  5. In the default constructor for MyDataGrid, add the line DefaultStyleKey = typeof(MyDataGrid);

If you want to access part of the style (which is defined in XAML) from code, you should name that XAML item as "PART_something". The design tools understand the PART_* syntax plus that's an indication to library consumers that if you retemplate the control, you must define the required pieces prefixed with PART_.

Here are the basics on how to define a custom control.

Upvotes: 1

Related Questions