Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug Problem setting CornerRadius in Generic.xaml
4 replies. Latest Post by Helen W on June 20, 2009.
(0)
Helen W
Member
150 points
87 Posts
06-20-2009 3:12 PM |
I have been working at creating a very simple Custom Control, using Jeff Prosise's MSDN Magazine article as a starting point.
It has been less than fun. Every time I run my little project, it errors out with
System.Windows.Markup.XamlParseException occurred Message="AG_E_UNKNOWN_ERROR [Line: 8 Position: 30]" LineNumber=8 LinePosition=30 StackTrace: at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at SimpleButtonDemo.Page.InitializeComponent() at SimpleButtonDemo.Page..ctor() InnerException:
Here is my entire generic.xaml file:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:custom="clr-namespace:SimpleButtonDemo;assembly=SimpleButtonDemo" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Style TargetType="custom:SimpleButton"> <Setter Property="Width" Value="200" /> <Setter Property="Height" Value="100" /> <Setter Property="Background" Value="Lavender" /> <Setter Property="BorderBrush" Value="Red" /> <Setter Property="BorderThickness" Value="4" /> <!--Setter Property="CornerRadius" Value="4" /--> <Setter Property="FontSize" Value="11" /> <Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Margin" Value="6" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="custom:SimpleButton"> <Grid x:Name="RootElement"> <Grid.Resources> <Storyboard x:Key="MouseEnterAnimation"> <DoubleAnimation Storyboard.TargetName="BorderElement" Storyboard.TargetProperty="Opacity" From="0" To="0.2" Duration="0:0:0.5" /> </Storyboard> <Storyboard x:Key="MouseLeaveAnimation"> <DoubleAnimation Storyboard.TargetName="BorderElement" Storyboard.TargetProperty="Opacity" From="0.2" To="0" Duration="0:0:0.5" /> </Storyboard> </Grid.Resources> <Border x:Name="BorderElement" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" CornerRadius="6" > <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}" Margin ="{TemplateBinding Margin}" ContentTemplate="{TemplateBinding ContentTemplate}" /> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
I finally tracked down the offending line:
<!--Setter Property="CornerRadius" Value="4" /-->
When I uncomment this line I get the error reported above. If I leave the line commented out, everything works fine.
Helen
SharpGIS
Contributor
3387 points
611 Posts
06-20-2009 4:53 PM |
Button does not have a CornerRadius property, and my guess is neither does your SimpleButton class: http://msdn.microsoft.com/en-us/library/system.windows.controls.button_properties(VS.95).aspx Create a CornerRadius dependency property on your SimpleButton class and set it on the Border element in the control template: CornerRadius="{TemplateBinding CornerRadius}".
06-20-2009 5:05 PM |
OK that makes sense. "SimpleButton" actually inherits from ContentControl, so what you are saying is that any property of a contained control in the template that I want to set needs to have a dependency property registered?
Is there a recommended way of handling such properties? For example, suppose my "SimpleButton" template contains two child controls (C1 & C2) that both have a "Width" property. Do I register 2 dependency properties for C1.Width and C2.Width? Is there a better way?
Thanks
06-20-2009 5:12 PM |
Any property on the superclass will also be available on the child control. For instance ContentControl already have a Width property, so there is no need to create this on your subclass control. However it doesn't have a CornerRadius property, so you will need to create that on your class. But unless you are not using it in your control template (I see you hardcoded "6" into the radius), there is no need for the property. But of course if you don't want to have it hardcoded, you will need to replace that hardcoded value with the TemplateBinding expression I showed above.
06-20-2009 5:20 PM |
I only hard-coded it because I couldn't get it to work. Will take your excellent advice and set it using template binding.
Thanks for the quick reply!