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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:17:08+00:00 2026-06-14T00:17:08+00:00

I’m pretty new to WPF, and I’m trying to write a custom control that

  • 0

I’m pretty new to WPF, and I’m trying to write a custom control that is basically a TextBlock, but also has a click event. I tried piecing the thing together from various sources, but it does not work.

In the following code, I would expect that OnMouseLeftButtonDown is called when a mouse click is performed on the element. If that happens, I want to raise the Click event. Looking in the debugger, the function is never called.

Did I misunderstand how this is supposed to work, or did some other error find it’s way into my code?

namespace EP3_gui.Controls
{
    public class ClickableTextBlock : TextBlock
    {
        public ClickableTextBlock() : base()
        { }

        public ClickableTextBlock( Inline inline )
            : base( inline )
        { }

        protected override void OnMouseLeftButtonDown( MouseButtonEventArgs e )
        {
            base.OnMouseLeftButtonDown( e );
            RoutedEventArgs args = new RoutedEventArgs( ClickEvent );
            RaiseEvent( args );
        }

        public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent(
            "Click",
            RoutingStrategy.Bubble,
            typeof( RoutedEventHandler ),
            typeof( ClickableTextBlock )
        );

        public event RoutedEventHandler Click
        {
            add { AddHandler( ClickEvent, value ); }
            remove { RemoveHandler( ClickEvent, value ); }
        }
    }
}
  • 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-14T00:17:09+00:00Added an answer on June 14, 2026 at 12:17 am

    As @menty pointed out, there is no error in the code, I made a mistake when testing the Control. In the XAML to test it I entered a value, because otherwise the control would have had no height and width (using Auto for sizes)

    My code was

     <cc:ClickableTextBlock x:Name="bit3" ... Click="bitText_Click" Text="{Binding Value}">0<cc:ClickableTextBlock>
    

    If one changes that to

     <cc:ClickableTextBlock x:Name="bit3" ... Click="bitText_Click" Text="{Binding Value}" />
    

    it works, but then one is not able to see the element in the designer. In my case it was very bad, since the size of the parent user control was set to auto, so the whole control looked wrong on the designer.

    The solution is to provide a default value for the binding:

     <cc:ClickableTextBlock x:Name="bit3" ... Click="bitText_Click" Text="{Binding Value, FallbackValue=0}" />
    

    This way it works, and the control is visible in the designer.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.