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

  • SEARCH
  • Home
  • 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 8728207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:34:53+00:00 2026-06-13T08:34:53+00:00

Control template: <ControlTemplate x:Key=BasicShape2> <StackPanel Name=sp> <Border Name=bd CornerRadius=3.5 BorderThickness=1 BorderBrush={Binding RelativeSource={RelativeSource TemplatedParent},Path=DataContext.NodeType, Converter={StaticResource

  • 0

Control template:

    <ControlTemplate x:Key="BasicShape2">
    <StackPanel Name="sp">
        <Border Name="bd" CornerRadius="3.5" BorderThickness="1" BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent},Path=DataContext.NodeType, Converter={StaticResource NodeTypeColorConverter}, Mode=OneWay}" Height="32" Padding="1">
            <TextBlock Name="tbName" Grid.Column="1" Text="" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" />
        </Border>
    </StackPanel>
</ControlTemplate>

a class which this template will apply to:

    public class MyThumbEx : Thumb
{
    public static readonly DependencyProperty MemberInfoProperty = DependencyProperty.Register("MemberInfo", typeof(FamilyMemberInfo), typeof(MyThumbEx));
    public FamilyMemberInfo MemberInfo
    {
        get { return (FamilyMemberInfo)GetValue(MemberInfoProperty); }
        set { SetValue(MemberInfoProperty, value); }
    }

    public MyThumbEx(ControlTemplate template, FamilyMemberInfo info, Point position)
    {
        this.MemberInfo = info;
        this.DataContext = this.MemberInfo;
        this.Template = template;
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        this.ApplyTextContent();
    }

    public void ApplyTextContent()
    {
        TextBlock tbName = this.Template.FindName("tbName", this) as TextBlock;
        if (tbName != null)
        {
            tbName.Text = this.MemberInfo.Name;
        }
    }
}

initialize and display it on a canvas:

        public MainWindow()
    {
        InitializeComponent();
        //
        FamilyMemberInfo mi = new FamilyMemberInfo();
        mi.Name = "someone";
        mi.ID = "id1";
        MyThumbEx te = new MyThumbEx(Application.Current.Resources["BasicShape2"] as ControlTemplate, mi, new Point(0, 0));
        //
        this.cvMain.Children.Add(te);
    }

These codes work fine, but be noticed that in the control template, I have to set Path=DataContext.NodeType, not just Path=NodeType. I’m new to WPF, and I found that normally, when I did binding without using this template stuff, I didn’t need to specify the predicate ‘DataContext’, right? Why we need here?

Another thing I found is, I can comment out this.DataContext = this.MemberInfo, and change binding path to Path=MemberInfo.NodeType, the code still works fine. Could anyone explain that for me?

Thanks in advance!

  • 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-13T08:34:55+00:00Added an answer on June 13, 2026 at 8:34 am

    If you dont change the DataContext manuelly, every child automatically has the DataContext of its Parent. So if your Window has f.e. the ViewModel as DataContext all of its Controls have access to the ViewModels Properties through {Binding Path=Property}.

    But in case of a ControlTemplate the usual typical flow where DataContext just cascades through from the parent to child doesn’t apply here. So you have to set the DataContext first, either through Property="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext.Property}" or DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}" Property="{Binding Path=Property}".

    To your second point: It could be, that the ControlTemplate automatically uses the code-behind of its containing Element as DataContext, so you can use the code-behinds properties without setting the DataContext, but I am not 100% sure about this.

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

Sidebar

Related Questions

Consider the following Control/Template <my:ExpandingListBox Margin=0,2,0,0 x:Name=TagSearchListBox> <my:ExpandingListBox.Template> <ControlTemplate TargetType={x:Type my:ExpandingListBox}> <Border Name=MyBorder BorderThickness=2
I have the following XAML/Control Template for a ListViewItem: <Window.Resources> <ControlTemplate x:Key=ItemTemplate TargetType=ListViewItem> <Border
I have a control template for a button that looks like this: <ControlTemplate x:Key=CopyButton
I have the following control template: <ControlTemplate x:Key=GlassButton TargetType={x:Type Button}> <ControlTemplate.Resources> <Storyboard x:Key=Timeline1> <DoubleAnimationUsingKeyFrames
my control template and style: <ControlTemplate TargetType={x:Type Button} x:Key=ImageButtonTemplate> <Image Source=..//..//images//ok.png Width={TemplateBinding Width} Height={TemplateBinding
I have defined following control template for my custom control. <ControlTemplate TargetType={x:Type local:CustomControl}> <Grid
I've run into a problem with data-binding inside control template while the property is
I've done it this way: <Style x:Key=Button BasedOn={StaticResource LoginButton} TargetType=Button> <Setter Property=Template> <Setter.Value> <ControlTemplate
I have a wpf cusotm control derived from combobox System.Windows.Controls.ComboBox the control template is
I have re-styled a textbox using a control template. The styling works well but

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.