Silverlight: Style for Custom Control in Same Project - silverlight

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!

It sounds like you made a custom control, but you haven't defined a default style for it. Try the following:
In your project, add a folder at the root level called Themes.
In the Themes folder, add a ResourceDictionary named generic.xaml.
Add all of your MyDataGrid styles to generic.xaml.
Make sure one of the styles is an implicit style (using BasedOn is a great solution here).
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.

Related

Ways to apply dynamic style to same user control in different silverlight application?

I have created a user control in silver-light.
I
will use this control in two different applications.
Each application has its own styling rules.
I want user control to adopt styling defined in the respective application.
One approach I found is
1) Define two separate styling template of same key in the App.xaml of both application.
2) Use static resource binding to bind styling to the control.
Is there other approach to do the same.??
If the style does not change within a single application I would define a style like this:
<Style TargetType="MyUserControl />
If it changes per situation/view, apply a resource key and set the style per instance of the control.

Get TreeView's styled expand/collapse icons

I need to render/draw the icons that the default TreeView uses for it's expand/collapse icons. I already found out that you can change them by writing a new style with x:Key="ExpandCollapseToggleStyle", but I have no idea how I could load that style from the currently loaded theme (e.g. Aero, Luna, etc.).
I already tried creating a ToggleButton and applying it's Style the StaticResource ExpandCollapseToggleStyle, but the resource can not be found:
<ToggleButton Style="{StaticResource ExpandCollapseToggleStyle}"></ToggleButton>
How can I apply styles defined in the default theme to controls they are not actually meant for?
it doesn't really work that way.
you have to extract the styles from that specific theme, copy them to your code and then reference them. Look here for how to extract the styles and templates. Martin Liversage's answer number 2 doesn't only work on silverlight assemblies. You can use Reflector (too bad it's commercial now...) to get the styles and themes from wpf dlls too.
btw: the corresponding ddls are named like PresentationFramework.Luna.dll

Templates in extended Silverlight controls

i am looking to extend a third party control, it is a ComboBox (so it isn't the standard Silverlight one, but that shouldn't matter for this question). To do this, i add a new template control to my controls project, then i change the new control to inherit from ComboBox instead of Control. A style has been created for me in the generic.xaml file, so i delete the default border stuff that was inserted, and then add a property setter for the PopupTemplate.
My problem is that when doing it this way, the combobox doesn't (visibly) render in the silverlight application anymore. However, in the constructor of my extension if i comment out the line
this.DefaultStyleKey = typeof(MyComboBox);
and put the PopupTemplate xaml bit in the main silverlight page, it renders correctly. I want the popup template to be declared within the control library, but does this mean that i also have to define the regular Template property? Am i wrong in thinking that anything i don't explicitly specify should just be inherited from the base control?
A control can only have one default style. You need to copy the entire default style of the base control into the Generic.Xaml for you new MyComboBox then adjust it accordingly.

What are your 'best practices' for writing custom controls in WPF

I have started writing some custom controls for a highly visual project. I was wondering what are your 'best practices' when coding WPF custom controls?
If you want your custom control to support direct content like this:
<CustomObject>
Direct content example 1
</CustomObject>
<!-- or -->
<CustomObject>
<Button Content="Direct content example 2" />
</CustomObject>
Then you need to use the ContentPropertyAttribute which tells WPF which property is actually being set when you write xaml like this.
The attribute can be used like this:
[ContentProperty("NameOfProperty")]
public class CustomObject
{
[...]
ContentControl uses this attribute to set the Content property but note that the property can be called anything; the WPF TextBox, for example uses this attribute to set the Text property.
E.G.
[ContentProperty("Text")]
The property also does not have to be a dependency property (see the MSDN documentation example for evidence of this).
Finally, this attribute is specific to the xaml parser and not to ContentControl and can be used with any type which can be seen from the TextBox example above (TextBox does not derive from ContentControl).
Keep property names the same as the property names for built-in Controls if you can do so without changing their meaning.
e.g. if you have a CustomerDisplayer custom control don't call the list of customers Customers, call it ItemsSource.
It might seem counter intuitive at first but it saves a lot of headache in the long run because future programmers can make a lot of assumptions about how a property called ItemsSource will act that they can't necessarily make about a Customers property.
Make sure the control can be re-styled and re-templated without changing the way the control operates. Don't make the control assume that the Listbox and Button are both within the same panel or that there even is a Listbox or Button. Check out the MSDN article on control authoring for some recommendations on how to do this.
Some content controls are dependant on the existence of other controls in their ControlTemplate. Typically this should be documented using the TemplatePart attribute.
The Combobox control, for example, is dependant on the existence of a TextBox and a Popup controls in its template.
This would be documented by placing the attribute on the class like so:
[TemplatePart(name="PART_EditableTextBox", type=typeof(TextBox))]
[TemplatePart(name="PART_Popup", type=typeof(Popup))]
public class Combobox : Selector
{
[...]
The naming convention is "PART_controlIdentifier".
The relevant items would then be given the same names in the control template so that they can be located in the OnApplyTemplate method.
This then allows the control to hook up to events, set properties and call methods on the controls contained in the template.
This attribute is for documentation purposes so that people designing custom control templates (and tools such as Expression Blend) know that the control is dependant on the existence of another.
Learn how to use both dependency properties and routed events (and how they work) so that you can use them effectively in your own control.
Both of these types provide services for integrating your control with the systems built into WPF.
By using these two features in your custom controls you will get the following advantages:
Dependency Properties provide support for databinding, animations and can be used in styles.
Routed Events can be propogated through the visual tree which means that other elements can handle the events.

Wpf Custom Control problem

I have strange problem with WPF custom control that I have made. When I drag and drop WPF custom control from toolbox to design surface I only see one border without any controls in it... I have another same WPF custom control project with same custom control and same code and when I reference that project everything works fine.
I have template in theme folder that is called DatePicker.Generic.Xaml and I have defined
<Style x:Key="{x:Type DateControls:DatePicker}"
TargetType="{x:Type DateControls:DatePicker}">
where DateControls is xml namespace of my DatePicker custom control.
I have also added
[assembly: ThemeInfo(
ResourceDictionaryLocation.SourceAssembly,
ResourceDictionaryLocation.SourceAssembly)]
in assemblyinfo file.
Can someone help?
Thanks
Check that your control code has a static constructor that calls DefaultStyleKeyProperty.OverrideMetadata with the appropriate type arguments (it's generated for you if you add a new custom control in VS).
You may want to look at the ComponentResourceKey Markup Extension. Look at the TypeInTargetAssembly and ResourceId argument (of componentresourcekey)'s when you set the Style element.

Resources