Wpf Custom Control problem - wpf

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.

Related

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!
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.

Can I make a custom control similar to a ContentControl editable in Blend?

I'm trying to make a custom control in Silverlight have the same functionality as a ContentControl, notably being editable in Blend.
The custom control has a property "AdditionalContent" which holds the Content that should be displayed. It is bound to and displayed with a ContentPresenter in the Xaml for the control's UI. Unfortunately, my custom control inherits from a 3rd party control, so inheriting from ContentControl is not an option.
I looked at the Silverlight Toolkit code, at HeaderedContentControl, and used it as guidance to get my "AdditionalContent" property working. The only problem is that it is not friendly to Blend. I'm getting some very basic editability in Blend, but not the smooth integration that Blend has for types of ContentControl or HeaderedContentControl.
Is there any kind of Attribute or something else I can add so Blend knows how to handle this situation? Or is it the case that Blend is hard coded for ContentControl and HeaderedContentControl types?
Add a [ContentPropertyAttribute] to the control specifying the name of the content element.

How to style a custom Silverlight 4 UserControl?

I have a custom UserControl that I created as a navigation menu that parses an xml file and populates itself with hyperlink buttons. So basically my control is an empty stackpanel, and when it's loaded it adds hyperlinkbuttons as children to the stack panel.
In my application I just add a <myLibrary:NavigationMenu links="somexml.xml" />
The problem is that I want to be able to style the hyperlinkbuttons and the stack panel differently for every application. What is the best way to do this.
In the code behind for the control, create a DependencyProperty of type Style for both HyperlinkStyle and StackPanelStyle. Then when you create the items apply the correct styles too them.
Take a look at MSDN
The article is a good starting point for writing stylable controls.

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.

Silverlight: How to style the validation tooltip?

I did some searching but it wasn't what I was looking for. So, does anyone know a way how to style the validation tooltip in Silverlight(the thing in the green border)?
alt text http://img689.imageshack.us/img689/222/validationtooltip.png
Any help would be greatly appreciated!
Best Regards,
~K
Unfortunately you can't easily provide a style for the validation tooltip without effectively styling the entire Textbox control. Blend makes this fairly easy if you are familiar with using that design tool. However if your a dyed in the wool coder like me then...
Goto this page on MSDN TextBox Styles and Templates
Copy the vsm namespace alias to your UserControl xaml
Copy the TextBox style into UserControls.Resources give it at an x:Key name (say MyTextBoxStyle")
Copy the ValidationToolTipTemplate from the web page to the UserControls.Resources, paste it above the TextBox style. It already has an x:Key name that the TextBox style will be referencing.
Add Style="{StaticResource MyTextBoxStyle}" to your TextBox in the data grid.
Now you play around with the validation elements of the templates to get your desired result.
The validation messages are displayed as a visual state. you can get to these (and edit them) as templates directly from blend.

Resources