Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - CustomControl and controlTemplate

 
 
Old 09-25-2007   #1 (permalink)
Eager


 
 

CustomControl and controlTemplate

I want to write a custom control which has a (dependency ?) property which
is an enum type, e.g. enum:.

public enum LookKindOfMyCustomControl
{
TwoRectanglesAndALine,
TwoEllipsesAndARectangle,
ThreeEllipsesAndTwoRectangles
}

(Just an example, the idea is that each of the items in the enumerator list
represents composite shapes, GeometryGroups, PathGeometry or similar which
is not easily represented as a parameterized "thing").

How can I best present this with a default style/template for the custom
control ?
By setting a particular ControlTemplate based on change of the property ?
How would the syntax be ?
Better done in code ? (but what about if an application wants to override
the look of the control ?)


My System SpecsSystem Spec
Old 09-27-2007   #2 (permalink)
Laurent Bugnion, MVP


 
 

Re: CustomControl and controlTemplate

Hi,

It's relatively easy to do if you use a Style trigger. For example (out
of the top of my head)

<Control.Style>

<Style TargetType="{x:Type Control}">

<Setter Property="Template">
<Setter.Value>
<!--Do something-->
</Setter.Value>
</Setter>

<Style.Triggers>
<DataTrigger Binding="{Binding MyLookAndFeel}"
Value="TwoRectanglesAndALine">
<Setter Property="Template">
<Setter.Value>
<!--Do something-->
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding MyLookAndFeel}"
Value="TwoEllipsesAndARectangle">
<Setter Property="Template">
<Setter.Value>
<!--Do something-->
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding MyLookAndFeel}"
Value="ThreeEllipsesAndTwoRectangles">
<Setter Property="Template">
<Setter.Value>
<!--Do something-->
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>

</Style>

</Control.Style>

To know if your property (which I named here MyLookAndFeel) must be a
DependencyProperty, you must know if the value may change during
runtime. If it's a DP, a change during runtime will be applied to the
control. If it's a normal (CLR) property, the value will be loaded when
the control is loaded, but subsequent changes will not be applied.

Greetings,
Laurent


Eager wrote:
Quote:

> I want to write a custom control which has a (dependency ?) property
> which is an enum type, e.g. enum:.
>
> public enum LookKindOfMyCustomControl
> {
> TwoRectanglesAndALine,
> TwoEllipsesAndARectangle,
> ThreeEllipsesAndTwoRectangles
> }
>
> (Just an example, the idea is that each of the items in the enumerator
> list represents composite shapes, GeometryGroups, PathGeometry or
> similar which is not easily represented as a parameterized "thing").
>
> How can I best present this with a default style/template for the custom
> control ?
> By setting a particular ControlTemplate based on change of the property
> ? How would the syntax be ?
> Better done in code ? (but what about if an application wants to
> override the look of the control ?)
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft.ch
PhotoAlbum: http://www.galasoft.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
My System SpecsSystem Spec
Old 09-27-2007   #3 (permalink)
Eager


 
 

Re: CustomControl and controlTemplate

Thanks a lot Laurent

I guess this means that XAML does not have something similar to a switch
statement (C#), but only if statements which will all be executed (the
trigger elements).


"Laurent Bugnion, MVP" <galasoft-lb@xxxxxx> wrote in message
news:usDj1nOAIHA.1204@xxxxxx
Quote:

> Hi,
>
> It's relatively easy to do if you use a Style trigger. For example (out of
> the top of my head)
>
> <Control.Style>
>
> <Style TargetType="{x:Type Control}">
>
> <Setter Property="Template">
> <Setter.Value>
> <!--Do something-->
> </Setter.Value>
> </Setter>
>
> <Style.Triggers>
> <DataTrigger Binding="{Binding MyLookAndFeel}"
> Value="TwoRectanglesAndALine">
> <Setter Property="Template">
> <Setter.Value>
> <!--Do something-->
> </Setter.Value>
> </Setter>
> </DataTrigger>
> <DataTrigger Binding="{Binding MyLookAndFeel}"
> Value="TwoEllipsesAndARectangle">
> <Setter Property="Template">
> <Setter.Value>
> <!--Do something-->
> </Setter.Value>
> </Setter>
> </DataTrigger>
> <DataTrigger Binding="{Binding MyLookAndFeel}"
> Value="ThreeEllipsesAndTwoRectangles">
> <Setter Property="Template">
> <Setter.Value>
> <!--Do something-->
> </Setter.Value>
> </Setter>
> </DataTrigger>
> </Style.Triggers>
>
> </Style>
>
> </Control.Style>
>
> To know if your property (which I named here MyLookAndFeel) must be a
> DependencyProperty, you must know if the value may change during runtime.
> If it's a DP, a change during runtime will be applied to the control. If
> it's a normal (CLR) property, the value will be loaded when the control is
> loaded, but subsequent changes will not be applied.
>
> Greetings,
> Laurent
>
>
> Eager wrote:
Quote:

>> I want to write a custom control which has a (dependency ?) property
>> which is an enum type, e.g. enum:.
>>
>> public enum LookKindOfMyCustomControl
>> {
>> TwoRectanglesAndALine,
>> TwoEllipsesAndARectangle,
>> ThreeEllipsesAndTwoRectangles
>> }
>>
>> (Just an example, the idea is that each of the items in the enumerator
>> list represents composite shapes, GeometryGroups, PathGeometry or similar
>> which is not easily represented as a parameterized "thing").
>>
>> How can I best present this with a default style/template for the custom
>> control ?
>> By setting a particular ControlTemplate based on change of the property ?
>> How would the syntax be ?
>> Better done in code ? (but what about if an application wants to override
>> the look of the control ?)
>
> --
> Laurent Bugnion [MVP ASP.NET]
> Software engineering, Blog: http://www.galasoft.ch
> PhotoAlbum: http://www.galasoft.ch/pictures
> Support children in Calcutta: http://www.calcutta-espoir.ch
My System SpecsSystem Spec
Old 09-28-2007   #4 (permalink)
Laurent Bugnion, MVP


 
 

Re: CustomControl and controlTemplate

Hi,

Eager wrote:
Quote:

> Thanks a lot Laurent
>
> I guess this means that XAML does not have something similar to a switch
> statement (C#), but only if statements which will all be executed (the
> trigger elements).
:-) I think that if you try to compare XAML to C#, you're going to have
long, sleepless nights. It's really a very different paradigm.

That said, you're right, in the sense that the triggers will all be
evaluated, but in that particular case, only one will be applied.

Greetings!
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft.ch
PhotoAlbum: http://www.galasoft.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
My System SpecsSystem Spec
 

Thread Tools



Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46