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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:46:03+00:00 2026-05-27T01:46:03+00:00

I have 3 textblocks in a stackpanel in a DataTemplate . the idea is

  • 0

I have 3 textblocks in a stackpanel in a DataTemplate.

the idea is that the format should be like “a b c” with spaces between the textblocks but it may happen that textblocks “a” and “b” might be empty, so i end up with something like this ” c” ( two spaces before textblock c). How can i ensure that i have a margin but only if there is content?

  • 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-27T01:46:04+00:00Added an answer on May 27, 2026 at 1:46 am

    If the TextBlock‘s Visibility is set to Collapsed, the Margin will not be shown. So I guess you can use a StringToVisibilityConverter to set its Visibility to Collapsed if the Text is empty.

    Example

    First you need to create a converter like this.

    public class StringToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return Visibility.Collapsed;
    
            if (value != null && string.IsNullOrWhiteSpace(value.ToString()))
                return Visibility.Collapsed;
    
            return Visibility.Visible;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Then you need to include this converter into your xaml.

    <converters:StringToVisibilityConverter x:Key="StringToVisibilityConverter " />
    

    Finally, use it inside your TextBlock.

    <TextBlock x:Name="MyTextBlock" Text="{Binding SomeText}"
             Visibility="{Binding SomeText, Converter={StaticResource StringToVisibilityConverter}}" />
    

    For more info regarding how to use a converter, please take a look at this post.

    Hope it helps. 🙂

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

Sidebar

Related Questions

Hei, So lets assume that I have this ListBox.ItemTemplate: <phone:PhoneApplicationPage.Resources> <DataTemplate x:Key=DataTemplate1> <StackPanel Orientation=Horizontal>
I have a combo box with the following DataTemplate: <DataTemplate x:Key=ComboBoxDataTemplate> <StackPanel> <TextBlock Text={Binding
I have this: <ListBox x:Name=PART_lstAttributes Grid.Row=1 Style={StaticResource GlossyBlackListBox}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock x:Name=txtAttributeName Text={Binding
My stackpanels have gaps between each item (TextBlocks). You can see through to whatever
We have two textBlocks like this: (we used .NET FW 3.0) <TextBlock Grid.Column=0 Name=tabName
I have the following xaml code in resources: <DataTemplate DataType={x:Type s:Substance}> <StackPanel Orientation=Horizontal> <TextBlock
I have almost tried everything but for some reason it is not working <StackPanel
I have a DataTemplate: <Style x:Key=SalesmanDestinyReportListBoxStyle TargetType=ListBox> <Setter Property=ItemsPanel> <Setter.Value> <ItemsPanelTemplate> <StackPanel Orientation=Horizontal />
I have a user control that i am using inside a DataTemplate , this
I have a DataTemplate that is bound to a buisness class, it also contains

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.