Skip to main content

Microsoft Silverlight

Answered Question Help -- Using {TemplateBinding Content} withTextBlock in ControlTemplateRSS Feed

(0)

berandor2008
berandor...

Member

Member

6 points

9 Posts

Help -- Using {TemplateBinding Content} withTextBlock in ControlTemplate

Basically I have a button that I built a control template for. It is currently working but VS 2010 complains about the following line of code in my control template

<TextBlock Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" x:Name="cContentTextBlock"  /> 

The control template is for a Button and I have a variety of VisualStates that target this TextBlock.
I can see why VS2010 complains... What if the content isn't actually text? This would cause problems. For me the big deal is that I want to set the Foreground of the text in response to Visual State changes.

Any ideas on how I could accomplish this? (try it, it works... but the vs2010 designer chokes on it)

The following is the entire style:
  

	<Style x:Key="PassiveLinkButton" TargetType="Button">
		<Setter Property="Cursor" Value="Hand" />
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate>
					<Grid>
						<vsm:VisualStateManager.VisualStateGroups>
							<vsm:VisualStateGroup x:Name="CommonStates">
								<vsm:VisualState x:Name="MouseOver">
									<Storyboard>
										<ObjectAnimationUsingKeyFrames Storyboard.TargetName="cMouseOverBorder" Storyboard.TargetProperty="BorderBrush">
											<DiscreteObjectKeyFrame KeyTime="0">
												<DiscreteObjectKeyFrame.Value>
													<SolidColorBrush Color="Black" />
												</DiscreteObjectKeyFrame.Value>
											</DiscreteObjectKeyFrame>
										</ObjectAnimationUsingKeyFrames>
									</Storyboard>
								</vsm:VisualState>
								<vsm:VisualState x:Name="Normal"/>
								<vsm:VisualState x:Name="Pressed">
									<Storyboard>
										<ObjectAnimationUsingKeyFrames Storyboard.TargetName="cMouseOverBorder" Storyboard.TargetProperty="BorderBrush">
											<DiscreteObjectKeyFrame KeyTime="0">
												<DiscreteObjectKeyFrame.Value>
													<SolidColorBrush Color="Blue" />
												</DiscreteObjectKeyFrame.Value>
											</DiscreteObjectKeyFrame>
										</ObjectAnimationUsingKeyFrames>
										<ObjectAnimationUsingKeyFrames Storyboard.TargetName="cContentTextBlock" Storyboard.TargetProperty="Foreground">
											<DiscreteObjectKeyFrame KeyTime="0">
												<DiscreteObjectKeyFrame.Value>
													<SolidColorBrush Color="Blue" />
												</DiscreteObjectKeyFrame.Value>
											</DiscreteObjectKeyFrame>
										</ObjectAnimationUsingKeyFrames>
									</Storyboard>
								</vsm:VisualState>
								<vsm:VisualState x:Name="Disabled"/>
							</vsm:VisualStateGroup>
						</vsm:VisualStateManager.VisualStateGroups>
						<Border x:Name="cFocusBorder" 
									BorderThickness="1" 
									BorderBrush="{StaticResource BorderBrush}"
									Margin="2">
							<Grid>
								<Grid.ColumnDefinitions>
									<ColumnDefinition Width="Auto" />
									<ColumnDefinition Width="*" />
								</Grid.ColumnDefinitions>
								<Border x:Name="cMouseOverBorder" 
									BorderBrush="Transparent" BorderThickness="0,0,0,1" Margin="0 0 0 2">
									<StackPanel HorizontalAlignment="Left">
										<TextBlock Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" 
													x:Name="cContentTextBlock" Margin="2 2 2 0"
													HorizontalAlignment="Center" />
									</StackPanel>
								</Border>
							</Grid>
						</Border>
					</Grid>
				</ControlTemplate>
			</Setter.Value>

		</Setter>
	</Style>
 

Ardman
Ardman

Contributor

Contributor

3434 points

946 Posts

Re: Help -- Using {TemplateBinding Content} withTextBlock in ControlTemplate

Are you using Beta 2 Visual Studio 2010?

berandor2008
berandor...

Member

Member

6 points

9 Posts

Re: Help -- Using {TemplateBinding Content} withTextBlock in ControlTemplate

The error was generated with VS 2010 Beta 2.  I am under the impression the error is correct but perhaps I am not right in this.

Min-Hong Tang - MSFT
Min-Hong...

Contributor

Contributor

3619 points

412 Posts

Answered Question

Re: Help -- Using {TemplateBinding Content} withTextBlock in ControlTemplate

Hi,

   Thanks a lot for notifing this issue. I have reported to visual studio team.

   Deeply sorry if it caused you any trouble.

Best Regards

Min-Hong Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

berandor2008
berandor...

Member

Member

6 points

9 Posts

Re: Help -- Using {TemplateBinding Content} withTextBlock in ControlTemplate

Hi Min-Hong,

So you believe my syntax is OK but it is an error in VS2010?  I ask because I am trying to determine if I need to not follow this particular practice.

 

Thanks !

Min-Hong Tang - MSFT
Min-Hong...

Contributor

Contributor

3619 points

412 Posts

Re: Help -- Using {TemplateBinding Content} withTextBlock in ControlTemplate

Hi Berandor,

   I test it in VS2008 both the desin view and the program works fine. And  in vs2010 the program also works fine but the design view gives us an exception.

Best Regards

Min-Hong Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities