Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit how do you use TemplateBinding with a Width(double) property with a "*" or "Auto" on a new DependencyProperty?
4 replies. Latest Post by xsirxx on October 8, 2008.
(0)
xsirxx
Member
28 points
40 Posts
10-06-2008 4:48 PM |
I have a control that has a DependencyProperty that sets a width. It is a double so setting actual values works just fine. But my problem is I want to be able to set percentages and Auto but I am bombing out on it.... here is the code, the property is PanelWidth...
// other control codepublic static readonly DependencyProperty PanelWidthProperty = DependencyProperty.Register("PanelWidth", typeof(double), typeof(SomeControl), null);public double PanelWidth{ get { return (double)GetValue(PanelWidthProperty); } set { SetValue(PanelWidthProperty, value); }}
// other control code
<!-- in generic.xaml --><Style TargetType="cyc:SomeControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="cyc:SomeControl"> <Grid x:Name="RootElement"> <Canvas x:Name="PanelElement" Width="{TemplateBinding PanelWidth}"> </Canvas> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
I set 'PanelElement's Width == PanelWidth. This is fine if the PanelWidth is an actual number but not Auto(NaN) or %s.... So my question is, how do I properly create the DependencyProperty or PanelWidth property to allow for NaNs or %s?
Thanks much!
--Brad
10-07-2008 12:06 PM |
*shameless bump*
To go into more depth incase I worded the quesstion badly(which I normally do), Say I were to add this line in the xaml:
<!-- in generic.xaml --> <Style TargetType="cyc:SomeControl">
<Setter Property="PanelWidth" Value="Auto"/>
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="cyc:SomeControl"> <Grid x:Name="RootElement"> <Canvas x:Name="PanelElement" Width="{TemplateBinding PanelWidth}"> </Canvas> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
The line "<Setter Property="PanelWidth" Value="Auto"/>" will not work because of PanelWidth being a double.... is there a way to create a custom property and set it as Auto?
Thanks again,
Brad
Yi-Lun L...
All-Star
25052 points
2,747 Posts
10-08-2008 5:57 AM |
Hello, the problem is, Canvas does not respect to Auto size. Try to use a Grid instead of Canvas. If you need percentage size, you can't use double. You need to use the GridLength type.
sanjay_sk16
329 points
79 Posts
10-08-2008 8:48 AM |
Hi xsirxx,
there is optiong to set width to auto you can use
like
double PanelWidth = double.NAN;
10-08-2008 10:51 PM |
PERFECT Yi-Lun Luo! That should work great! You always know the answers!
Thanks very much,