Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9171767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:12:29+00:00 2026-06-17T16:12:29+00:00

Having mocked up a custom control as a window and got all the behaviour

  • 0

Having mocked up a custom control as a window and got all the behaviour right, I’m now trying to turn it into a proper custom control (called “When”, it’s a date-time widget).

I have prepared a When.XAML file in which sub-elements are named PART_xxx

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:glob="clr-namespace:System.Globalization;assembly=mscorlib"
  xmlns:local="clr-namespace:Widgets">
  <local:DatePartPositionValueConverter x:Key="DatePartPositionValueConverter" />
  <local:DatePartVisibilityValueConverter x:Key="DatePartVisibilityValueConverter" />
  <Style TargetType="{x:Type local:When}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:When}">
          <Border Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}">
            <Border BorderThickness="1" BorderBrush="{DynamicResource 
                {x:Static SystemColors.ControlDarkBrushKey}}">
              <Grid HorizontalAlignment="Left" Margin="4,0,0,0">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition />
                  ...
                  <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock x:Name="PART_year" Grid.Column="{Binding 
                    Converter={StaticResource DatePartPositionValueConverter}, 
                    ConverterParameter=y}">
                  <TextBlock.Text>
                  ...

The custom control project Generic.XAML file references When.XAML

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Widgets;component/Themes/When.xaml" />
  </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

My code, however, does not seem to be able to resolve the PART_ names.

I’d like to be able to compare _focussedElement to (eg) PART_year to provide context for validation checks. It must be possible to refer directly to PART_xxx from the code of a custom control, otherwise it would be impossible to use code to bind event handlers to the elements of a template.

What have I failed to apprehend?


To paraphrase and expand on the excellent answer below, PART_year isn’t in scope because there’s no opportunity for the IDE’s code generation magic to bring it into scope. So you bring it into scope yourself, like this:

MenuItem PART_MenuItemToday, PART_MenuItemNow, 
  PART_MenuItemMonthEnd, PART_MenuItemMonthStart;
public override void OnApplyTemplate()
{
  base.OnApplyTemplate();
  PART_MenuItemMonthEnd = GetTemplateChild("PART_ContextMenuMonthEnd") as MenuItem;
  PART_MenuItemMonthEnd.Click += PART_ContextMenuMonthEnd_Click;
  ...
}

When you need to hook the same set of handlers to several widgets you can do this

private void BindGenericHandlers(TextBlock textBlock)
{
  textBlock.GotFocus += PART_GotFocus;
  textBlock.LostFocus += PART_LostFocus;
  textBlock.MouseDown += PART_MouseDown;
  textBlock.MouseEnter += PART_MouseEnter;
  textBlock.MouseLeave += PART_MouseLeave;
}

TextBlock _focussedElement, PART_year, PART_month, PART_day, PART_hour, PART_minute, PART_second, PART_designator;

public override void OnApplyTemplate()
{
  base.OnApplyTemplate();
  BindGenericHandlers(PART_day = GetTemplateChild("PART_day") as TextBlock);
  BindGenericHandlers(PART_designator = GetTemplateChild("PART_designator") as TextBlock);
  BindGenericHandlers(PART_hour = GetTemplateChild("PART_hour") as TextBlock);
  BindGenericHandlers(PART_minute = GetTemplateChild("PART_minute") as TextBlock);
  BindGenericHandlers(PART_month = GetTemplateChild("PART_month") as TextBlock);
  BindGenericHandlers(PART_second = GetTemplateChild("PART_second") as TextBlock);
  BindGenericHandlers(PART_year = GetTemplateChild("PART_year") as TextBlock);
  ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:12:30+00:00Added an answer on June 17, 2026 at 4:12 pm

    You retreive references to your PARTs in the code by using the GetTemplateChild method inside the OnApplyTemplate override. So in the code for your control, you’ll have the following:

    private const string PART_TEXTINPUT = "PART_TEXT";
    private TextBox _textInput;
    
    public override void OnApplyTemplate()
    {
       base.OnApplyTemplate();
       _textInput = GetTemplateChild(PART_TEXTINPUT) as TextBox;
    }
    

    Since you are using PARTs, you seem to be making a lookless control, and therefore, you can’t have a direct reference to the element from the XAML (since a custom ControlTemplate could replace it with something unexpected). So you retrieve references to your PARTs with the GetTemplateChild method.

    NOTE: Be sure to use the lowest possible type for the part (in your code) in case someone replaces your expected control with a different implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having used storyboards for a while now I have found them extremely useful however,
I'm trying to (unit) test my EJB class without having to startup my websphere
Right now, if you have a test that looks like this: [TestMethod] [DeploymentItem(DataSource.csv)] [DataSource(
Having trouble trying to declare a variable within a var namespace .. jsFiddle ..
In one of my unit tests, I am having some difficulty getting a mocked
I'm using the Setup() method to set up the behaviour of a mocked instance
Having searched a whole lot of similair posts, workarounds, I decided to make my
Having trouble with each function... Will try to explain by example... In my code,
Having a hard time with labels on a ggplot2 plot. Here's a similar plot
Having just added a new button in my web application, I get an error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.