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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:20:56+00:00 2026-05-26T10:20:56+00:00

I have several StackPanels that change visibility based on ToggleButtons. The code below works

  • 0

I have several StackPanels that change visibility based on ToggleButtons. The code below works if I replace Tag with btn1 on the DataTrigger-lines.
How do I use the value of the Tag property?

<Window x:Class="MyTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TestApp">

    <Window.Resources>
        <Style x:Key="panelStyle" TargetType="{x:Type StackPanel}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=Tag, Path=IsChecked}" Value="False">
                    <Setter Property="StackPanel.Visibility" Value="Collapsed" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=Tag, Path=IsChecked}" Value="True">
                    <Setter Property="StackPanel.Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>

    <WrapPanel>
        <ToggleButton Content="One" Name="btn1" />
        <ToggleButton Content="Two" Name="btn2" />

        <StackPanel Style="{StaticResource panelStyle}" Tag="{Binding btn1}">
            <Label Content="Data to panel 1" />
        </StackPanel>

        <StackPanel Style="{StaticResource panelStyle}" Tag="{Binding btn2}">
            <Label Content="Data to panel 2" />
        </StackPanel>

    </WrapPanel>

</Window>    

This question is very similar, but I’m missing details on how to pass an element name.
XAML – Generic textbox stylewith triggers / parameters?

  • 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-26T10:20:57+00:00Added an answer on May 26, 2026 at 10:20 am

    Your bindings are incorrect.

    In your DataTemplate the bindings should be:

    <DataTrigger Binding="{Binding Path=Tag.IsChecked, RelativeSource={RelativeSource Self}}" Value="False">
       <Setter Property="StackPanel.Visibility" Value="Collapsed" />
    </DataTrigger>
    

    Here the RelativeSource with a mode of Self tells the binding engine that the object to bind against is object to which the style is being applied (e.g. your StackPanel). The PropertyPath of Tag.IsChecked tells the binding engine to look for a property called IsChecked from the object stored in Tag.

    Finally the bindings in your StackPanel should be:

    <StackPanel Style="{StaticResource panelStyle}" Tag="{Binding ElementName=btn1}">
       <Label Content="Data to panel 1" />
    </StackPanel>
    

    Here ElementName creates a binding to another element in the logical tree. If you do not explicitly assign to any properties in a Binding as in your original example:

    Tag="{Binding btn1}"
    

    The value specified is assigned to the Path property. So this would be the same as:

    Tag="{Binding Path=btn1}"
    

    Also note, that using Tag is not considered best practice since it’s type is of object and its use is unrestricted, and hence can take on any number of different meanings throughout your project (which often makes it difficult to understand, especially when used in Templates that are located far away from their actual use).

    Hope this helps!

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

Sidebar

Related Questions

I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on
We have several jobs that run concurrently that have to use the same config
I have several old 3.5in floppy disks that I would like to backup. My
I have several applications that are part of a suite of tools that various
We have several .NET applications that monitor a directory for new files, using FileSystemWatcher.
I have several controllers that require a correct user for their edit/update/delete actions. What
I have several small classes that are all peers of each other declared and
I have several threads that write to the same int. Each thread increment the
I have an Expander that its content consists of a StackPanel that contains several
I have a treeview in wpf that is built using the xaml below. It

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.