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 6089967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:09:35+00:00 2026-05-23T12:09:35+00:00

I’m getting the following Binding errors on my code and I don’t know how

  • 0

I’m getting the following Binding errors on my code and I don’t know how to troubleshoot them. The bindings were generated by VS. I’ve tried adding presentation.tracesources (which is in the code below) but I get the same output as before.

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='ClimateSolutions.SuperTB', AncestorLevel='1''. BindingExpression:Path=myName; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='ClimateSolutions.SuperTB', AncestorLevel='1''. BindingExpression:Path=isRequired; DataItem=null; target element is 'SuperTB' (Name='email'); target property is 'NoTarget' (type 'Object')

Here’s my XAML:

<TextBox x:Class="ClimateSolutions.SuperTB"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" Height="53" Width="296" FontSize="32"
         xmlns:local="clr-namespace:ClimateSolutions"
xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"

         HorizontalAlignment="Left" Name="Blarg">
<TextBox.Style>
    <Style TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="Text" Value="">
                <Setter Property="Background">
                    <Setter.Value>
                        <VisualBrush Stretch="None">
                            <VisualBrush.Visual>
                                <TextBlock Foreground="Gray" FontSize="24" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=local:SuperTB, AncestorLevel=1}, Path=myName, diagnostics:PresentationTraceSources.TraceLevel=High}">
                                </TextBlock>
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <DataTrigger Binding="{Binding Path=isRequired, RelativeSource={RelativeSource FindAncestor, AncestorType=local:SuperTB, AncestorLevel=1}}" Value="False">
                <Setter Property="Text" Value="100" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBox.Style>

and here’s the CS for SuperTB:

namespace ClimateSolutions
{
    /// <summary>
    /// Interaction logic for SuperTB.xaml
    /// </summary>
    public partial class SuperTB : TextBox, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String Property)
        {
            var anEvent = this.PropertyChanged;

            if (anEvent != null)
            {
                anEvent(this, new PropertyChangedEventArgs(Property));
            }
        }

        private String MyName = "Unicorns!";

        private static DependencyProperty myNameProperty = DependencyProperty.Register("myName", typeof(String), typeof(SuperTB));
        public String myName
        {
            get { return MyName; }
            set { MyName = value; NotifyPropertyChanged("myName"); }
        }

        DependencyProperty isRequiredProperty = DependencyProperty.Register("isRequired", typeof(Boolean), typeof(SuperTB));

        public Boolean isRequired
        {
            get { return (Boolean)GetValue(isRequiredProperty); }
            set { SetValue(isRequiredProperty, value); }
        }

        public SuperTB()
        {
            InitializeComponent();
            myName = "Unicorns!";
        }
    }
}
  • 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-05-23T12:09:36+00:00Added an answer on May 23, 2026 at 12:09 pm

    EDIT : I have updated the code according to your comment. To summarize, since this is a custom control, you are less dependant on the MVVM pattern to build your component logic (and thus use code behind in you component) as soon as your componennt itself meets this needs (to be sort, make its properties to be as much bindable as you can). For example, in the updated code, you can now bind the default property, but you can also imagine exposing properties to set the foreground colors used to diaplay control name when there is no value, and so forth.


    I tried several things with you original code (included solution provided by J cooper) and nothing seemed to work. It seems that there is a lot of issues with your code.

    I managed to approach a solution by making your textbox a custom control.

    Here is the Generic.xaml (the visual definition of your control) :

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Question_6514447">
    
    
    <Style TargetType="{x:Type local:SuperTB2}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:SuperTB2}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <TextBox x:Name="PART_Input">
                            <TextBox.Style>
                                <Style TargetType="TextBox">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsRequired}" Value="False">
                                            <Setter Property="Text" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DefaultTextValue}" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    </ResourceDictionary>
    

    And here is the code behind of the control :

     [TemplatePart(Name = "PART_Input")]
    public class SuperTB2 : Control
    {
        private TextBox PART_Input;
        static SuperTB2()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SuperTB2), new FrameworkPropertyMetadata(typeof(SuperTB2)));
        }
    
        public SuperTB2()
        {
            Loaded += SuperTb2Loaded;
        }
        public override void OnApplyTemplate()
        {
            PART_Input = GetTemplateChild("PART_Input") as TextBox;
            if (PART_Input != null)
            {
    
    
                PART_Input.GotFocus += PartInputGotFocus;
                PART_Input.LostFocus += PartInputLostFocus;
            }
        }
    
        void PartInputLostFocus(object sender, RoutedEventArgs e)
        {
            if (PART_Input.Text == string.Empty)
            {
                PART_Input.Text = Name;
                PART_Input.Foreground = new SolidColorBrush(Colors.Gray);
            }
        }
    
        void PartInputGotFocus(object sender, RoutedEventArgs e)
        {
            if (PART_Input.Text.Equals(Name))
            {
                PART_Input.Text = string.Empty;
                PART_Input.Foreground = new SolidColorBrush(Colors.Black);
    
            }
        }
    
        void SuperTb2Loaded(object sender, RoutedEventArgs e)
        {
            if (PART_Input.Text == string.Empty)
            {
                PART_Input.Text = Name;
                PART_Input.Foreground = new SolidColorBrush(Colors.Gray);
            }
        }
    
        private static DependencyProperty myNameProperty =
    DependencyProperty.Register("MyName", typeof(string), typeof(SuperTB2), new PropertyMetadata("Unicorns !", NameChanged));
    
        private static void NameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
    
        }
    
        public string MyName
        {
            get { return (string)GetValue(myNameProperty); }
            set { SetValue(myNameProperty, value); }
        }
        DependencyProperty isRequiredProperty =
            DependencyProperty.Register("IsRequired", typeof(bool), typeof(SuperTB2), new PropertyMetadata(false, IsReqChanged));
    
        private static void IsReqChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
    
        }
    
        public bool IsRequired
        {
            get { return (bool)GetValue(isRequiredProperty); }
            set { SetValue(isRequiredProperty, value); }
        }
    
    
    
        public string DefaultTextValue
        {
            get { return (string)GetValue(DefaultTextValueProperty); }
            set { SetValue(DefaultTextValueProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for DefaultTextValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DefaultTextValueProperty =
            DependencyProperty.Register("DefaultTextValue", typeof(string), typeof(SuperTB2), new UIPropertyMetadata("100"));
    
    
    }
    

    And an example of use of the component :

    <Grid>
        <StackPanel>
            <Question_6514447:SuperTB2 x:Name="FirstName" IsRequired="true" DefaultTextValue="200"/>
        </StackPanel>
    </Grid>
    

    With this updated code, I think you can acheive almost all the behaviors your needed !

    Hope this will help !

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.