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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:19:35+00:00 2026-05-24T04:19:35+00:00

Sorry for the terrible Question Title, I don’t know how to phrase this, feel

  • 0

Sorry for the terrible Question Title, I don’t know how to phrase this, feel free to comment.

I am working on a WPF desktop app using the MVVM pattern. I have a great number of text fields on my ViewModel, all of which I want to display using the pattern below (greatly simplified):

<StackPanel Orientation="Vertical">
    <StackPanel.Style>
        <Style TargetType="{x:Type StackPanel}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=SomePredicate}" Value="False">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>

    <Label Content="SomeHeader:"/>

    <TextBlock Text="{Binding Path=SomeText}" />

</StackPanel>

The display always consists of:

  • A Predicate for determining if the StackPanel should be shown
  • A string used as header text. This can be set in XAML, it doesn’t have to come from the viewmodel
  • A binding to the Textblock text string on the ViewModel

I would really love to be able to define these like this:

<MyHeaderedTextBlockControl Text="{Binding Path=SomeText}"
                            Header="SomeHeader:"
                            Predicate="{Binding SomePredicate}"/>

Can this be done? I tried doing it with a UserControl but had no idea what I was doing.

It is important to me that the binding modes remain working, ie if the Text binding is in OneWay or TwoWay mode, the TextBlock should update when the Text property on the ViewModel raises OnPropertyChanged.

I also dont like the idea of doing this with a View and a ViewModel for each such text property, because then I have to create those ViewModels and wire them up for updates etc. I want a solution that I can implement in the View, ideally the ViewModel shouldn’t even know about it.

  • 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-24T04:19:36+00:00Added an answer on May 24, 2026 at 4:19 am

    With UserControls you define dependency properties in your code and forward them to the “template” via binding, e.g.:

    <UserControl x:Class="Test.UserControls.HeaderedTextBlock"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 Name="control">
        <StackPanel Orientation="Vertical">
            <StackPanel.Style>
                <Style TargetType="{x:Type StackPanel}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Predicate, ElementName=control}" Value="False">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Style>
            <Label Content="{Binding Header, ElementName=control}" />
            <TextBlock Text="{Binding Text, ElementName=control}" />
        </StackPanel>
    </UserControl>
    
    namespace Test.UserControls
    {
        public partial class HeaderedTextBlock : UserControl
        {
    
            public static readonly DependencyProperty HeaderProperty =
                DependencyProperty.Register("Header", typeof(string), typeof(HeaderedTextBlock), new UIPropertyMetadata(null));
            public string Header
            {
                get { return (string)GetValue(HeaderProperty); }
                set { SetValue(HeaderProperty, value); }
            }
    
            public static readonly DependencyProperty PredicateProperty =
                DependencyProperty.Register("Predicate", typeof(bool), typeof(HeaderedTextBlock), new UIPropertyMetadata(false));
            public bool Predicate
            {
                get { return (bool)GetValue(PredicateProperty); }
                set { SetValue(PredicateProperty, value); }
            }
    
            public static readonly DependencyProperty TextProperty =
                DependencyProperty.Register("Text", typeof(string), typeof(HeaderedTextBlock), new UIPropertyMetadata(null));
            public string Text
            {
                get { return (string)GetValue(TextProperty); }
                set { SetValue(TextProperty, value); }
            }
    
            public HeaderedTextBlock()
            {
                InitializeComponent();
            }
        }
    }
    

    Would something like this work for you?

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

Sidebar

Related Questions

Sorry, I know this question is easy, but I don't know how to get
I am sorry for the terrible description of this question. I am making a
Sorry if this question is framed incorrectly. I am working on an iPad application
[edit]: I'm terribly sorry for my timing on asking this question. I've just discovered
Sorry for this simple question, but I can't solve it... There is an example:
Sorry for the bad title - I simply do not know what to call
Sorry but I am not sure how to ask this question but I am
Sorry if this question seems stupid, but it's stumped me for a couple days,
Sorry for the terrible title, best I could think of at the time! Say
Sorry for the vague title, but I wasn't sure how to summarize this in

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.