![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Output not collected from synthetic setter methods | PowerShell | |||