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 - Why can't I use TemplateBinding in Setter?

 
 
Old 04-11-2006   #1 (permalink)
HolaMan


 
 

Why can't I use TemplateBinding in Setter?

Hi~~~

I want to use some TemplateBinding in a setter of ControlTemplate's
Triggers.
But the runtime throwed the exception to warn me I can't do that.


<Style x:Key="{x:Type local:BitmapButton}" TargetType="{x:Type
local:BitmapButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:BitmapButton}">
<Grid x:Name="Grid" RenderTransformOrigin="0.5,0.5">
.... etc
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source"
Value="{TemplateBinding
local:BitmapButton.ImageDefault}"
^^^^^^^^^^^^^^^^^^^^^^
TargetName="Image"/>

Why I can't use TemplateBinding in Setters?

So I change the way and write triggers on C# code.
In OnApplyTemplate() function

Trigger newTrigger = new Trigger();
newTrigger.Property = BitmapButton.IsMouseOverProperty;
newTrigger.Value = true;
Setter setter1 = new Setter(Image.SourceProperty, new
BitmapImage(...etc);
newTrigger.Setters.Add(setter1);
this.Triggers.Add(newTrigger);

But I got a exception that only EventTrigger can be added
into Button.Triggers.

Could anyone suggest other ways to implement that?
Thanks

have a nice day


HolaMan


My System SpecsSystem Spec
Old 04-12-2006   #2 (permalink)
Nick Kramer [MSFT]


 
 

Re: Why can't I use TemplateBinding in Setter?

Have you tried {Binding} instead of {TemplateBinding}? I honestly don't
know if that works, you may not inherit the DataContext into that place.
But as for Binding vs. TemplateBinding, TemplateBinding is intended as a
subset of Binding, created for a few specific scenarios we knew how to
optimize specially.

-Nick Kramer [MSFT]

---
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"HolaMan" <reality.chang@gmail.com> wrote in message
news:1144753729.285998.87760@z34g2000cwc.googlegroups.com...
> Hi~~~
>
> I want to use some TemplateBinding in a setter of ControlTemplate's
> Triggers.
> But the runtime throwed the exception to warn me I can't do that.
>
>
> <Style x:Key="{x:Type local:BitmapButton}" TargetType="{x:Type
> local:BitmapButton}">
> <Setter Property="Template">
> <Setter.Value>
> <ControlTemplate TargetType="{x:Type local:BitmapButton}">
> <Grid x:Name="Grid" RenderTransformOrigin="0.5,0.5">
> .... etc
> <ControlTemplate.Triggers>
> <Trigger Property="IsMouseOver" Value="True">
> <Setter Property="Source"
> Value="{TemplateBinding
> local:BitmapButton.ImageDefault}"
> ^^^^^^^^^^^^^^^^^^^^^^
> TargetName="Image"/>
>
> Why I can't use TemplateBinding in Setters?
>
> So I change the way and write triggers on C# code.
> In OnApplyTemplate() function
>
> Trigger newTrigger = new Trigger();
> newTrigger.Property = BitmapButton.IsMouseOverProperty;
> newTrigger.Value = true;
> Setter setter1 = new Setter(Image.SourceProperty, new
> BitmapImage(...etc);
> newTrigger.Setters.Add(setter1);
> this.Triggers.Add(newTrigger);
>
> But I got a exception that only EventTrigger can be added
> into Button.Triggers.
>
> Could anyone suggest other ways to implement that?
> Thanks
>
> have a nice day
>
>
> HolaMan
>



My System SpecsSystem Spec
Old 04-13-2006   #3 (permalink)
HolaMan


 
 

Re: Why can't I use TemplateBinding in Setter?

Thanks for your reply.

But It seems that Binding doesn't work either.

<local:MyImageSourceConverter x:Key="ImgConverter"/>

<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<StackPanel>
<TextBlock x:Name="tb" Text="Test"/>
<Image Source="{TemplateBinding Button.Content,
Converter={StaticResource ImgConverter}}"/>
<Rectangle Width="100" Height="100" x:Name="RR">
<Rectangle.Fill>
<ImageBrush x:Name="Test"
ImageSource="d:\\p1.jpg"/>
</Rectangle.Fill>
</Rectangle>
</StackPanel>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Text" Value="{Binding
Path=Content}" TargetName="tb"/>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

But I found I can use a strange way to do that. Set a collapsed
object(in order not to affect the final result) outside to
templatebinding
and use Binding's "ElementName" to bind the value.

<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<StackPanel>
<TextBlock x:Name="tb2" Text="{TemplateBinding
Button.Content}" Visibility="Collapsed"/>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Collapsed Object but TemplateBinding
<TextBlock x:Name="tb" Text="Test"/>
<Image Source="{TemplateBinding Button.Content,
Converter={StaticResource ImgConverter}}"/>
<Rectangle Width="100" Height="100" x:Name="RR">
<Rectangle.Fill>
<ImageBrush x:Name="Test"
ImageSource="d:\\p1.jpg"/>
</Rectangle.Fill>
</Rectangle>
</StackPanel>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Rectangle.Fill" Value="{Binding
Path=Text, ElementName=tb2, Converter={StaticResource ImgConverter}}"
TargetName="RR"/>


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Binding to Collapsed Properties
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


Or if I miss some easier method or syntax to do the same thing?
Thanks

Have a nice day

HolaMan

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Output not collected from synthetic setter methods PowerShell


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