*Usual Disclaimer - i have searched hi and lo for a solution, as per maybe i just lack google skills*
I'm trying to create a control derived from a button, which will allow the user to specify a (double) amount to rotate the (text) content (leaving all other UI elements alone). I created this class:
public class ArcButton : Button
{
public static readonly DependencyProperty ContentRotationProperty =
DependencyProperty.Register("ContentRotation", typeof(double), typeof(Button), null);
public double ContentRotation
{
get { return (double) GetValue(ContentRotationProperty); }
set { SetValue(ContentRotationProperty, value); }
}
}
I created a custom UI, then in modified the style to include this xaml:
You are probably right - the ownertype property was wrong - but it has no effect on the application when corrected. I think the reason being (after more googling):
Thanks - i think if the post in the reply to .netdan below was correct, then no binding whether in Xaml or code behind will work if in SL3 transforms are not framework elements. But i think you are right. Code behing without binding is the answer...
Solved. The solution which worked for me was to create a converter to return a RotateTransform:
public
class
RotationConverter :
IValueConverter
{
public
object Convert(object value,
Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
if (value
is
ArcButton)
{
RotateTransform rotateTransform =
new
RotateTransform();
rotateTransform.Angle = (value as
ArcButton).ContentRotation;
return rotateTransform;
}
return
null;
}
public
object ConvertBack(object value,
Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw
new
NotImplementedException();
}
}
downtrodden
0 Points
4 Posts
Control Templates and Databinding
Jul 31, 2009 05:09 AM | LINK
*Usual Disclaimer - i have searched hi and lo for a solution, as per maybe i just lack google skills*
I'm trying to create a control derived from a button, which will allow the user to specify a (double) amount to rotate the (text) content (leaving all other UI elements alone). I created this class:
I created a custom UI, then in modified the style to include this xaml:
This has no effect. If i remove the TemplateBinding and replace with a value e.g. 115, the content (text) is rotated correctly.
If i run in debug mode after reverting to the TemplateBinding, the ContentRotation setter is called.
I read this post
http://blogs.msdn.com/marlat/archive/2009/05/13/silverlight-3-template-binding-vs-relative-binding.aspx
and noted the line: Automatic Conversion is not supported in template binding, so with priority being int type, that won’t work.
I then tried this approach using a binding and a converter e.g:
{Binding RelativePath={RelativePath TemplatedParent},Path=ContentRotation,Converter={StaticResource DoubleConverter}}.
But this gave me invalid XAML errors in Blend.
Help!
mrjvdveen
Contributor
2285 Points
426 Posts
Re: Control Templates and Databinding
Jul 31, 2009 07:37 AM | LINK
The last way of doing relative binding is only supported in WPF and not in Silverlight (I had to find that out the hard way too).
You could bind the Angle property to the ContentRotation property in code behind. For an example on how to do this, check this blog post.
It does it with a ComboBox.SelectedItem property, but the same method applies here.
HTH.
Please mark a post as answer if it answers your question.
Visit my blog: http://jvdveen.blogspot.com
.netdan
Contributor
3655 Points
547 Posts
Re: Control Templates and Databinding
Jul 31, 2009 07:39 AM | LINK
Hi,
You put:
Shouldn't it be:
?
Dan Birch
MCSD
Free Silverlight,HTML5 Games, Controls and CMS | Free HTML5 iPhone, Android Games
downtrodden
0 Points
4 Posts
Re: Re: Control Templates and Databinding
Aug 01, 2009 03:35 AM | LINK
Hi
You are probably right - the ownertype property was wrong - but it has no effect on the application when corrected. I think the reason being (after more googling):
http://silverlight.net/forums/t/38924.aspx
Assuming this is still the case in Silverlight 3?
Thanks for replying.
downtrodden
0 Points
4 Posts
Re: Re: Control Templates and Databinding
Aug 01, 2009 03:39 AM | LINK
mrjvdveen
Thanks - i think if the post in the reply to .netdan below was correct, then no binding whether in Xaml or code behind will work if in SL3 transforms are not framework elements. But i think you are right. Code behing without binding is the answer...
downtrodden
0 Points
4 Posts
Re: Control Templates and Databinding
Aug 01, 2009 07:03 AM | LINK
Solved. The solution which worked for me was to create a converter to return a RotateTransform:
public class RotationConverter : IValueConverter{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is ArcButton)
{
RotateTransform rotateTransform = new RotateTransform();
rotateTransform.Angle = (value as ArcButton).ContentRotation;
return rotateTransform;
} return null;
} public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
then trigger it with the following XAML:
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RenderTransform="{Binding Converter={StaticResource RotateConverter},RelativeSource={RelativeSource TemplatedParent}}">
</ContentPresenter>