How to Inherit (Extend) TextBlock in Silverlight?

Posted by Girish578 under Silverlight on 3/24/2010 | Views : 6370 | Status : [Member] | Replies : 5
How to Inherit (Extend) TextBlock in Silverlight?




Responses

Posted by: Vuyiswamb on: 3/24/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
look at this
http://forums.silverlight.net/forums/t/101760.aspx

http://betaforums.silverlight.net/forums/t/113667.aspx

Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Girish578, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Girish578 on: 3/24/2010 [Member] Starter

Up
0
Down
Yes, I already visited those weblinks, but still having problem. If possible can u give me an example code?

Girish578, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 3/24/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
but did you look at this
http://betaforums.silverlight.net/forums/t/113667.aspx

the code example has been provided and the guy is happy that ,please check that

Thank you for posting at Dotnetfunda
[Administrator]

Girish578, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Girish578 on: 3/24/2010 [Member] Starter

Up
0
Down
Yes, when I use this as

created
public class MyTextBlock : Control

registered as xmlns:ctrl="..."

and declared it as
<ctrl:MyTextBlock
Text="My Text"/>

It's not working. And where to add style xaml code (generic.xaml) ? in the same control library project ? if so added in the control library, i am I doing anything wrong?

Girish578, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Girish578 on: 3/25/2010 [Member] Starter

Up
0
Down
I have added the following code..

Step 1:-
---------------
public class MyTextBlock:Control
{
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}

public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MyTextBlock), null);


public MyTextBlock()
{
DefaultStyleKey = typeof(MyTextBlock);
}

TextBlock innerTB;
public override void OnApplyTemplate()
{
innerTB = GetTemplateChild("textblock1") as TextBlock;
}

// set layout logic by handling MeasureOverride/ArrangeOverride.
protected override Size ArrangeOverride(Size finalSize)
{
return base.ArrangeOverride(finalSize);
}
protected override Size MeasureOverride(Size availableSize)
{
return base.MeasureOverride(availableSize);
}
}

Step2:-
------------
generic.xaml
----------------

<Style TargetType="my:MyTextBlock">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="my:MyTextBlock">
<TextBlock Name="textblock1"
Foreground="{TemplateBinding Foreground}"
FontSize="{TemplateBinding FontSize}"
Text="{TemplateBinding Text}"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


But When I keep the control in MainPage as

<my:MyTextBlock Text="My Text"/>

MyTextBlock is not visible in the browser. What is the problem I don't know.

We have created dependency property, template.... still we have this problem.
please help me. If possible can anyone test and if worked please attach the zip file.


Girish578, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response