Apply a control template to a thumb with c#

M

moondaddy

I have a wpf app and need to apply a control template to a thumb at runtime,
but I can't do it the same way I would apply a style. this is what I want
to do:

Thumb thm = new Thumb();
thm.Template = TryFindResource("myThumbTemplate") as Template;

but of course, this doesn't work.

Any advise?

Thanks.
 
L

Linda Liu[MSFT]

Hi George,

I performed a test based on your description but didn't reproduce the
problem on my side. I could set the Template property of a Thumb control at
run time.

The following is XAML:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300" Loaded="Window_Loaded" >
<Window.Resources>
<ControlTemplate x:Key="ThumbTemplate" TargetType="{x:Type Thumb}">
<Ellipse Width="20" Height="20" Fill="Blue"/>
</ControlTemplate>
</Window.Resources>
<StackPanel>
<Canvas Name="myCanvasStretch" Width="200" Height="200"
Background="Pink">
<Thumb Name="myThumb" Canvas.Left="80" Canvas.Top="80"
Background="Blue"
Width="20" Height="20" />
</Canvas>
</StackPanel>
</Window>

The code in the Window1.cs file:

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.myThumb.Template = TryFindResource("ThumbTemplate") as
ControlTemplate;
}
}

Build and run the application. The Thumb control is rendered as a blue
ellipse.

Is there any difference between your code and mine?

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

moondaddy

Thanks Linda,

you gave the answer I was looking for:

= TryFindResource("ThumbTemplate") as ControlTemplate;

I wasnt using as "ControlTemplate" which is why it failed.
 
L

Linda Liu[MSFT]

Hi George,

Thank you for your confirmation! I'm glad to hear that the problem is
solved now.

If you have any other questions in the future, please don't hesitate to
contact us. It's always our pleasure to be of assistance!

Have a nice weekend!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top