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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:53:31+00:00 2026-05-18T23:53:31+00:00

Consider the following piece of Xaml <Grid Background=Blue> <Border Width=100 Height=60 BorderBrush=Black BorderThickness=2> <Border

  • 0

Consider the following piece of Xaml

<Grid Background="Blue">
    <Border Width="100" Height="60" BorderBrush="Black" BorderThickness="2">
        <Border Background="Red">
            <Border.OpacityMask>
                <VisualBrush>
                    <VisualBrush.Visual>
                        <TextBlock Text="Text"
                                   Foreground="#FF000000"
                                   Background="#00000000"/>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Border.OpacityMask>
        </Border>
    </Border>
</Grid>

It will look like this because of the OpacityMask whos only non-transparent part is the Foreground of the TextBlock.
alt text

Now if I switch the Colors for Foreground and Background in the TextBlock like this

<TextBlock Text="Text"
           Foreground="#00000000"
           Background="#FF000000"/>

I get this because the even though the Foreground is transparent the Background behind it is not, resulting in a useless OpacityMask 🙂
alt text

Is there anyway I can get this? Basically an inverted OpacityMask
alt text

Am I missing some other way to do this here?

Update
To clarify, even though my example is about a TextBlock, it could be anything. Ellipse/Image/Path etc. The feature I’m after is “Invert OpacityMask”

  • 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-18T23:53:31+00:00Added an answer on May 18, 2026 at 11:53 pm

    You can use my HollowTextBlock which is different answer to the same question:

    <Grid Background="Blue">
        <Border Width="100" Height="60" BorderBrush="Black" BorderThickness="2">
            <Border Background="Red">
                <Border.OpacityMask>
                    <VisualBrush Stretch="None">
                        <VisualBrush.Visual>
                            <local:HollowTextBlock Width="200" Height="50" Text="Text" Background="White" HorizontalAlignment="Center"/>
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Border.OpacityMask>
            </Border>
        </Border>
    </Grid>
    

    Update:

    Here is a more fleshed out version of HollowTextBlock with proper measure capability, property value inheritance for the usual text properties, and a new VerticalTextAlignment property for centering the text vertically in it’s allocated space:

    public class HollowTextBlock : FrameworkElement
    {
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
    
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(HollowTextBlock), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(HollowTextBlock.OnTextChanged), new CoerceValueCallback(HollowTextBlock.CoerceText)));
    
        public Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }
    
        public static readonly DependencyProperty BackgroundProperty =
            TextElement.BackgroundProperty.AddOwner(typeof(HollowTextBlock), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
    
        public double FontSize
        {
            get { return (double)GetValue(FontSizeProperty); }
            set { SetValue(FontSizeProperty, value); }
        }
    
        public static readonly DependencyProperty FontSizeProperty =
            TextElement.FontSizeProperty.AddOwner(typeof(HollowTextBlock));
    
        public FontFamily FontFamily
        {
            get { return (FontFamily)GetValue(FontFamilyProperty); }
            set { SetValue(FontFamilyProperty, value); }
        }
    
        public static readonly DependencyProperty FontFamilyProperty =
            TextElement.FontFamilyProperty.AddOwner(typeof(HollowTextBlock));
    
        public FontStyle FontStyle
        {
            get { return (FontStyle)GetValue(FontStyleProperty); }
            set { SetValue(FontStyleProperty, value); }
        }
    
        public static readonly DependencyProperty FontStyleProperty =
            TextElement.FontStyleProperty.AddOwner(typeof(HollowTextBlock));
    
        public FontWeight FontWeight
        {
            get { return (FontWeight)GetValue(FontWeightProperty); }
            set { SetValue(FontWeightProperty, value); }
        }
    
        public static readonly DependencyProperty FontWeightProperty =
            TextElement.FontWeightProperty.AddOwner(typeof(HollowTextBlock));
    
        public FontStretch FontStretch
        {
            get { return (FontStretch)GetValue(FontStretchProperty); }
            set { SetValue(FontStretchProperty, value); }
        }
    
        public static readonly DependencyProperty FontStretchProperty =
            TextElement.FontStretchProperty.AddOwner(typeof(HollowTextBlock));
    
        public TextAlignment TextAlignment
        {
            get { return (TextAlignment)GetValue(TextAlignmentProperty); }
            set { SetValue(TextAlignmentProperty, value); }
        }
    
        public static readonly DependencyProperty TextAlignmentProperty =
            Block.TextAlignmentProperty.AddOwner(typeof(HollowTextBlock));
    
        public VerticalAlignment VerticalTextAlignment
        {
            get { return (VerticalAlignment)GetValue(VerticalTextAlignmentProperty); }
            set { SetValue(VerticalTextAlignmentProperty, value); }
        }
    
        public static readonly DependencyProperty VerticalTextAlignmentProperty =
            DependencyProperty.Register("VerticalTextAlignment", typeof(VerticalAlignment), typeof(HollowTextBlock), new FrameworkPropertyMetadata(VerticalAlignment.Top, FrameworkPropertyMetadataOptions.AffectsRender));
    
        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OnTextChanged(d, (string)e.NewValue);
        }
    
        private static void OnTextChanged(DependencyObject d, string newText)
        {
        }
    
        private static object CoerceText(DependencyObject d, object baseValue)
        {
            return baseValue;
        }
    
        protected override Size MeasureOverride(Size availableSize)
        {
            var face = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
            var size = FontSize;
            var ft = new FormattedText(Text, Thread.CurrentThread.CurrentUICulture, FlowDirection.LeftToRight, face, size, Brushes.Black);
            return new Size(ft.Width, ft.Height);
        }
    
        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            var extent = new RectangleGeometry(new Rect(0.0, 0.0, RenderSize.Width, RenderSize.Height));
            var face = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
            var size = FontSize;
            var ft = new FormattedText(Text, Thread.CurrentThread.CurrentUICulture, FlowDirection.LeftToRight, face, size, Brushes.Black);
            var originX = GetHorizontalOrigin(ft.Width, RenderSize.Width);
            var originY = GetVerticalOrigin(ft.Height, RenderSize.Height);
            var hole = ft.BuildGeometry(new Point(originX, originY));
            var combined = new CombinedGeometry(GeometryCombineMode.Exclude, extent, hole);
            drawingContext.PushClip(combined);
            drawingContext.DrawRectangle(Background, null, new Rect(0.0, 0.0, RenderSize.Width, RenderSize.Height));
            drawingContext.Pop();
        }
    
        private double GetHorizontalOrigin(double textWidth, double renderWidth)
        {
            switch (TextAlignment)
            {
                case TextAlignment.Center:
                    return (renderWidth - textWidth) / 2;
                case TextAlignment.Left:
                    return 0;
                case TextAlignment.Right:
                    return renderWidth - textWidth;
            }
            return 0;
        }
    
        private double GetVerticalOrigin(double textHeight, double renderHeight)
        {
            switch (VerticalTextAlignment)
            {
                case VerticalAlignment.Center:
                    return (renderHeight - textHeight) / 2;
                case VerticalAlignment.Top:
                    return 0;
                case VerticalAlignment.Bottom:
                    return renderHeight - textHeight;
            }
            return 0;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following piece of code #include<iostream> #include<string> class A { private: char name[10];
Consider the following piece of code - class MyThread extends Thread { private int
Consider the following piece of code: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void)
Consider the following piece of code: 1 typedef std::deque<int> mydeque_t; 2 mydeque_t mydeque; 3
Consider the following piece of LaTeX code: \begin{tabular}{p{1in}p{1in}} A & B\\ C & D\\
Consider the following piece of code: int i, k, m; k = 12; m
Consider the following piece of C++0x code: a_signal.connect([](int i) { if(boost::any_cast<std::string>(_buffer[i]) == foo) {
Consider the following piece of code: As you can see we are on line
Consider the following piece of code : <select> <option value=0>foo</option> <option value=1 selected=selected>bar</option> </select>
Consider the following piece of code: $tests = array( array (a, b, c), array

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.