Apply a control template to a thumb with c#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • moondaddy

    Apply a control template to a thumb with c#

    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 ("myThumbTempla te") as Template;

    but of course, this doesn't work.

    Any advise?

    Thanks.

    --
    moondaddy@newsg roup.nospam


  • Linda Liu[MSFT]

    #2
    RE: Apply a control template to a thumb with c#

    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="WpfApp lication1.Windo w1"
    xmlns="http://schemas.microso ft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microso ft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300" Loaded="Window_ Loaded" >
    <Window.Resourc es>
    <ControlTemplat e x:Key="ThumbTem plate" TargetType="{x: Type Thumb}">
    <Ellipse Width="20" Height="20" Fill="Blue"/>
    </ControlTemplate >
    </Window.Resource s>
    <StackPanel>
    <Canvas Name="myCanvasS tretch" Width="200" Height="200"
    Background="Pin k">
    <Thumb Name="myThumb" Canvas.Left="80 " Canvas.Top="80"
    Background="Blu e"
    Width="20" Height="20" />
    </Canvas>
    </StackPanel>
    </Window>

    The code in the Window1.cs file:

    public partial class Window1 : Window
    {
    public Window1()
    {
    InitializeCompo nent();
    }

    private void Window_Loaded(o bject sender, RoutedEventArgs e)
    {
    this.myThumb.Te mplate = 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:
    msdnmg@microsof t.com.

    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    Gain technical skills through documentation and training, earn certifications and connect with the community

    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/subscripti...t/default.aspx.
    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.


    Comment

    • moondaddy

      #3
      Re: Apply a control template to a thumb with c#

      Thanks Linda,

      you gave the answer I was looking for:

      = TryFindResource ("ThumbTemplate ") as ControlTemplate ;

      I wasnt using as "ControlTemplat e" which is why it failed.



      "Linda Liu[MSFT]" <v-lliu@online.mic rosoft.comwrote in message
      news:Mdz1bfmuIH A.4512@TK2MSFTN GHUB02.phx.gbl. ..
      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="WpfApp lication1.Windo w1"
      xmlns="http://schemas.microso ft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microso ft.com/winfx/2006/xaml"
      Title="Window2" Height="300" Width="300" Loaded="Window_ Loaded" >
      <Window.Resourc es>
      <ControlTemplat e x:Key="ThumbTem plate" TargetType="{x: Type Thumb}">
      <Ellipse Width="20" Height="20" Fill="Blue"/>
      </ControlTemplate >
      </Window.Resource s>
      <StackPanel>
      <Canvas Name="myCanvasS tretch" Width="200" Height="200"
      Background="Pin k">
      <Thumb Name="myThumb" Canvas.Left="80 " Canvas.Top="80"
      Background="Blu e"
      Width="20" Height="20" />
      </Canvas>
      </StackPanel>
      </Window>
      >
      The code in the Window1.cs file:
      >
      public partial class Window1 : Window
      {
      public Window1()
      {
      InitializeCompo nent();
      }
      >
      private void Window_Loaded(o bject sender, RoutedEventArgs e)
      {
      this.myThumb.Te mplate = 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:
      msdnmg@microsof t.com.
      >
      =============== =============== =============== =====
      Get notification to my posts through email? Please refer to
      Gain technical skills through documentation and training, earn certifications and connect with the community

      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/subscripti...t/default.aspx.
      =============== =============== =============== =====
      This posting is provided "AS IS" with no warranties, and confers no
      rights.
      >
      >

      Comment

      • Linda Liu[MSFT]

        #4
        Re: Apply a control template to a thumb with c#

        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:
        msdnmg@microsof t.com.

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


        Comment

        Working...