WPF - TextBlock - Cannot override OnRender - wpf

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

Related

Create a reusable user control with custom templates

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.

Why WPF controls don't have Tag property?

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.

How to make custom WPF ContentControl that supports drag-and-drop at design time?

I want to create custom WPF control that has a single "child" control inside. Subclassing ContentControl or UserControl works, but has one flaw: these controls don't work in designer mode.
By "don't work" I mean this scenario: suppose I have a Canvas with my custom control in it. I want to put, say, a Button inside my control. I drag it from the toolbox, and it appears inside my control. However, XAML view shows that the new button actually belongs to Canvas, not to my control.
I can place it inside my control by manually editing XAML, but I want the designer to work too.
Interestingly, when I subclass Canvas, Grid or Panel, designer works as expected. However, these controls have many children, which is not what I need.
How can I make a single-child control that works in designer?
how about inheriting from Border? that way you could spare yourself the hassle with Designer Extensibility
I had the same problem with a content control I am writing and found an easy solution on this
StackOverflow thread.
Just implement the HitTestCore method:
protected override System.Windows.Media.HitTestResult HitTestCore(System.Windows.Media.PointHitTestParameters hitTestParameters)
{
return new PointHitTestResult(this, hitTestParameters.HitPoint);
}
I also had a similar question here.
But after digging and digging it seams that the real answer is "NO", there isn't any official way to support dragging controls straight into a custom Content-Control at Design-Time, even implementing 'HitTestCore' as Stephan's answer suggests, does not enable drag&drop at design-time for ContentControl.
For such purposes you should consider inheriting from either Grid or Panel (to allow multiple child controls), or Border (to allow single child).

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.

WPF: Custom Window

There are many articles that described how to create a custom shaped window in WPF but none of them (at least I can't find any) described how can build a reusable one such as inheriting from it in other windows.
I have tried to create a ControlTemplate from my custom window. The problem is with the Close button and the MoveDrag() method.
If I wire the event to one of my other controls in ControlTemplate their Parent property is null, so I can not drag or close the window.
Does anyone have any ideas how can I create a reusable custom window template or something?
Unfortunately there is no such things as visual inheritance in WPF. (no xaml inheritance to be more specific)
For your specific issue, instead of inheriting, you could create a CustomForm as a template (with a big empty container in the middle), and then create all your other forms as usercontrols that fill that container.
The following will return the window object containing the control:
Window.GetWindow(myControl)

Resources