|
|
|
date: Tue, 25 Sep 2007 23:05:44 +0200,
group: microsoft.public.windows.developer.winfx.avalon
back
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:
> 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
date: Thu, 27 Sep 2007 11:36:59 +0200
author: Laurent Bugnion, MVP
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" wrote in message
news:usDj1nOAIHA.1204@TK2MSFTNGP03.phx.gbl...
> 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:
>> 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
date: Thu, 27 Sep 2007 17:12:30 +0200
author: Eager am
Re: CustomControl and controlTemplate
Hi,
Eager wrote:
> 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
date: Fri, 28 Sep 2007 09:06:46 +0200
author: Laurent Bugnion, MVP
|
|