I am curious how I can add a property to add functionality to a control. Currently I am just extending controls, but I was curious if it is possible to add a property to add functionality to a control. Like for example the ToolTipService. You can add that to controls. Would it be possible for me to add a property to add a contextmenu without having to extend a textbox lets say? I know about behaviors, but is it possible to do that as a property? Let's say I add a property IsContextMenuBehaviorAdded="True" and that will just attach a behavior, or just add functionality. Any help would be greatly appreciated. Thanks in advance.
If you are looking to add a context menu to a control then you can do that with the Silverlight Toolkit which contains a ContextMenuService.
Related
What is the difference between Custom Control and Custom Behavior?
Where Custom Control should be used and where Custom Behavior should be. in what ways they can be best used?
Behaviour extends control functionalities
Custom controls customize the visual of Control
A behaviour can be used to extend the functionality of a control to do something that it cannot do on its own without having to modify or re-implement the entire control.
Consider for example the built-in TreeView control in WPF. It has a SelectedItem property that is read-only which means that you cannot two-way bind it to a property of your view model. If you don't want to implement your own custom TreeView control from scratch just because of this - which you probably don't :) - you could solve this by implementing a custom behaviour that sets the value of your source property whenever the value of the target property changes and vice versa. Please refer to the following blog post for more information about this and an example: https://blog.magnusmontin.net/2014/01/30/wpf-using-behaviours-to-bind-to-readonly-properties-in-mvvm/
The following article should also provide a good introduction to attached behvaiours: https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF
So a behaviour is basically a piece of code that can be attached to some element in the XAML markup of a view through attached properties and add additional functionality to this element.
A control is a UI component that encapsulates some functionality and has a template such, as for example a Button or a ListBox.
Behaviors encapsulate pieces of functionality into a reusable component.
Custom controls are more work than custom behaviors(White box vs black box)
Some things you have to do a custom control, eg Access Protected members.
I made a user control, and in my control I have "Title", graphically "Title" is a TextBlock, but I want those who use mon control will have the option to change it, so, the first idea is the create a property "TitleTemplate" that inherits from "DataTemplate", I'm just following the wpf logic, but I cant find any tutorial that can help me.
I think, u should read more about TemplateBinding and ContentPresenter.
I suggest u read this articles:
Using Templates to Customize WPF Controls
and
Dr.WPF
After some research, I have concluded that the best way is the create a CustomControl, not a UserControl, because, my control is not a composition of other controls, I hope that helps.
I think all WinForm controls have a Tag property which I found very convenient on occasions. However, this Tag property seems to be gone for WPF controls like DataGrid. Can anybody explain why? Is there a better way in WPF to accomplish this?
WPF controls do have a Tag-property (Because of the base-class). However with the concept of the DataContext, the tag-property is not anymore very important.
The Tag property misses meaning. You can use Attached Properties to create more semantic properties on any control.
I am creating a custom control by deriving TextBlock, my intention is to do some custom rendering based on some dependency properties. However the OnRender method is sealed on TextBlock. Although I can get my work done by overriding OnRenderSizeChanged, this is not correct. Any ideas on how can i do it the right way?
Thanks in advance.
In WPF you normally work with styles or Control templates to change the appearance of a control. You can download templates for all controls from Microsoft and play around with them.
Read more here:
http://msdn.microsoft.com/en-us/library/system.windows.controls.controltemplate(v=VS.100).aspx
I'm trying to derive from the Silverlight Panel control to add some custom logic whenever a control is added to the Panel. I can't seem to find an "OnChildAdded" event (Or something similar) on the Children collection. Does anyone have any suggestions for how I can tell when a child control is added to a Panel or do I have to write my own container control?
Thanks,
Paul
Asked before here - unfortunately you can't get notification with the existing UIElementCollection Children property, but you could try a custom implementation.