Skip to main content

Microsoft Silverlight

Answered Question how do you use TemplateBinding with a Width(double) property with a "*" or "Auto" on a new DependencyProperty?RSS Feed

(0)

xsirxx
xsirxx

Member

Member

28 points

40 Posts

how do you use TemplateBinding with a Width(double) property with a "*" or "Auto" on a new DependencyProperty?

 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 code

public 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

xsirxx
xsirxx

Member

Member

28 points

40 Posts

Re: how do you use TemplateBinding with a Width(double) property with a "*" or "Auto" on a new DependencyProperty?

 *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 Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: how do you use TemplateBinding with a Width(double) property with a &quot;*&quot; or &quot;Auto&quot; on a new DependencyProperty?

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.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

sanjay_sk16
sanjay_sk16

Member

Member

329 points

79 Posts

Re: how do you use TemplateBinding with a Width(double) property with a &quot;*&quot; or &quot;Auto&quot; on a new DependencyProperty?

 Hi xsirxx,

there is optiong to set width to auto you can use

like

double PanelWidth = double.NAN;

 

Mr. Sanjay S. Kadam
(Jr. Software Developer)
[AksTech Solutions Pvt Ltd.]

Mark it as answer if its helpful for you.

xsirxx
xsirxx

Member

Member

28 points

40 Posts

Re: how do you use TemplateBinding with a Width(double) property with a &quot;*&quot; or &quot;Auto&quot; on a new DependencyProperty?

 PERFECT Yi-Lun Luo!  That should work great!  You always know the answers!

 

Thanks very much,

Brad

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities