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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:12:13+00:00 2026-06-01T01:12:13+00:00

I am new to WPF data binding / style / templates… I am trying

  • 0

I am new to WPF data binding / style / templates…
I am trying to apply a set of property values to buttons by using a Style. The style binds to fields of a class. As you can see, this works fine for the BackColor property. But when I try to set the Text of a TextBlock, it does not work (neither do I get a binding error ). My ultimate goal is to also be able to set an Image as the content.

When I do not use a DataTemplate, and use SetterProperty=”Content” instead of “ContentTemplate”, it will work for one button, but when adding a second button it gives me a runtime error “Specified element is already the logical child of another element. Disconnect it first.”

What am I missing here? What do I put into “TextBlock Text=”???”

btw. I would like to move the style to a global scope once it works so I do not want to use anything that explicitly refers to MyClass

<Window.Resources>
    <Style TargetType="Button" x:Key="MyStyle">
        <Setter Property="Background" Value="{Binding BackColor}"/>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="XYZ-"/>
                        <TextBlock Text="{Binding Text}"/>
                    </StackPanel>                        
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<StackPanel Orientation="Horizontal" Height="30">
    <Button Style="{StaticResource MyStyle}" DataContext="{Binding Action1}"/>
    <Button Style="{StaticResource MyStyle}" DataContext="{Binding Action1}"/>
    <Button Style="{StaticResource MyStyle}" DataContext="{Binding Action2}"/>
    <Button Style="{StaticResource MyStyle}" DataContext="{Binding Action2}"/>
</StackPanel>

and

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;

        Action1 = new MyClass() { Text = "ACTION1", BackColor = new SolidColorBrush(Colors.Red) };
        Action2 = new MyClass() { Text = "ACTION2", BackColor = new SolidColorBrush(Colors.Green) };
    }
    public MyClass Action1{get; private set;}
    public MyClass Action2{get; private set;}
}

public class MyClass
{
    public string Text { get; set; }
    public Brush BackColor { get; set; }
}
  • 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-01T01:12:14+00:00Added an answer on June 1, 2026 at 1:12 am

    In your original question, you needed

    <Setter Property="Background" Value="{Binding BackColor}"/>
    <Setter Property="Content" Value="{Binding Text}"/>
    

    Now you need to use a relative source binding

    <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
               AncestorType=Button}, Path=DataContext.Text}"/>
    

    However you may be better off using an ItemsControl as follows

    Xaml

    <Page.Resources>
        <DataTemplate x:Key="ItemTemplate" DataType="{x:Type Samples:MyClass}">
            <Button Background="{Binding BackColor}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="XYZ-"/>
                    <TextBlock Text="{Binding Text}"/>
                </StackPanel>
            </Button>
        </DataTemplate>
      <ItemsPanelTemplate x:Key="ItemsPanelTemplate">
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </Page.Resources>
    <Page.DataContext>
        <Samples:DataTemplateItemsControlViewModel/>
    </Page.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ItemsControl 
            ItemsSource="{Binding Items}" 
            ItemsPanel="{StaticResource ItemsPanelTemplate}"
            ItemTemplate="{StaticResource ItemTemplate}"/>
    </Grid>
    

    C#

    public class DataTemplateItemsControlViewModel
    {
        public DataTemplateItemsControlViewModel()
        {
            Items =
                new Collection<MyClass>
                    {
                        new MyClass
                            {
                                Text = "ACTION1", 
                                BackColor = new SolidColorBrush(Colors.Red)
                            },
                        new MyClass
                            {
                                Text = "ACTION2", 
                                BackColor = new SolidColorBrush(Colors.Blue)
                            },
                        new MyClass
                            {
                                Text = "ACTION3", 
                                BackColor = new SolidColorBrush(Colors.Green)
                            },
                        new MyClass
                            {
                                Text = "ACTION4", 
                                BackColor = new SolidColorBrush(Colors.Yellow)
                            },
                    };
        }
    
        public IList<MyClass> Items { get; private set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to WPF and I'm trying to figure out how data binding works,
I'm new to WPF and data binding so hopefully I can explain the problem
I'm VERY new to WPF, and still trying to wrap my head around binding
I am somewhat new to WPF and Data Binding, it seems very powerful. I'm
I'm new to WPF data binding. I have a ListBox on a form that
We need to assign the Name property at the runtime using the data binding.
I am just starting out with WPF and am trying to understand data binding.
I have a situation where I am using wpf data binding and validation using
Okay, so I'm fairly new to WPF and data binding, but I've searched and
I'm kind of new to WPF, and I'm trying to do some specialized data

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.