Question

I'm working on a custom WPF control based on ContentControl that is supposed to show more less important information as its size increases. Basically the Control contains a grid that can have more columns when the control has a sufficient width to display that information and less columns otherwise.

Therefore I somehow need to override some event i guess in which I can determine the actual size of the control and then rebuild its Grid thats defined in the ContentTemplate.

So the actual questions are: Which event should I override to achieve this? And How do I gain access to the Grid defined in the DataTemplate? In fact this one sounds a bit wrong as logic and UI should be separated and one should be able to replace the entire ControlTemplate without affecting the logic behind it...

Regards, Max

Was it helpful?

Solution

You should consider posting us more code when you ask such specific questions.

Which event should I override to achieve this?

You seem to be asking for SizeChanged event:

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.sizechanged(v=vs.110).aspx

Any FE (FrameworkElement) has it therefore you will be able to subscribe to it.


And How do I gain access to the Grid defined in the DataTemplate?

In order to find something inside a template you could follow this constillation:

this.Template.FindName("theScrollViewer", this) as ScrollViewer

In my example I seek for a element with the name theScrollViewer

EDIT:

There is a great explaination here:

http://msdn.microsoft.com/en-us/library/bb613586(v=vs.110).aspx

EDIT2:

[TemplatePart(Name="PART_ContentHost", Type=typeof(FrameworkElement))]

TemplatePart is just an attribute that visual studio will use to show you how you should give names to your controls inside the template.


This method is doing the same as I showed you above.

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    anchor = Template.FindName("PART_ContentHost", this) as FrameworkElement;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top