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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:48:21+00:00 2026-05-23T18:48:21+00:00

I have the following code. In my Example I have Displaying Buttons using ItemControl

  • 0

I have the following code.

In my Example I have Displaying Buttons using ItemControl.
Now i need to access particular button based on its content
and change its property.

say,, i nee to change Background property of first button when ChangeBackGround button being Clicked. [See Screen shot at very below at my Question]

Have a look at below code and Screen Shot :

XAML:

<Window x:Class="ItemControlProblem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50*" />
            <RowDefinition Height="261*" />
        </Grid.RowDefinitions>
        <ItemsControl Name="itemControlProblem" ItemsSource="{Binding Path=DataCollection}" Grid.RowSpan="2">
            <ItemsControl.Template>
                <ControlTemplate TargetType="ItemsControl">
                    <Border>
                        <StackPanel>
                            <ItemsPresenter></ItemsPresenter>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"></StackPanel>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Path=DataToPresent}"></Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <Button Content="ChangeBackGround" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="215,104,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

CODE BEHIND:

public partial class MainWindow : Window
    {
        private ObservableCollection<Data> dataCollection ;

        public MainWindow()
        {
            InitializeComponent();
            dataCollection = new ObservableCollection<Data>();

            dataCollection.Add(new Data("first"));
            dataCollection.Add(new Data("second"));
            dataCollection.Add(new Data("third"));
            dataCollection.Add(new Data("forth"));
            this.DataContext = this;
        }

        public ObservableCollection<Data> DataCollection
        {
            get
            {
                return this.dataCollection;
            }
            set
            {
                this.dataCollection = value;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //I need to change Background of Perticular Button
            //Based On Content of Button [Here that is "first", "second", "third", "forth"]

            //How to change the color of perticular button based on Content of Button?
        }
    }

    public class Data 
    {
        private string dataToPresent;

        public Data()
        {

        }
        public Data(string dataArg)
        {
            this.dataToPresent = dataArg;
        }
        public string DataToPresent 
        {
            get
            {
                return this.dataToPresent;
            }
            set
            {
                this.dataToPresent = value;
            }
        }
    }

Screen Shot:

enter image description here

  • 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-23T18:48:22+00:00Added an answer on May 23, 2026 at 6:48 pm

    Use this method to find visual children:

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }
    
                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }
    

    Example:

    var buttonFirst = FindVisualChildren<Button>(itemControlProblem)
        .Single(b => b.Content.ToString() == "first");
    buttonFirst.Background = Brushes.Yellow;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code and now I have type, but I need to have
I have the following example code: class A(object): def __init__(self, id): self.myid = id
Say I have the following code: <div onclick='location.href=http://www.example.com/'> <a href='#' onclick='alert(blah)'>click</a> </div> Is there
On a report I have the following code for a field: =Sum([PartQty]*[ModuleQty]) Example results
Say suppose I have the following Java code. public class Example { public static
I have the following code example (see below). My problem is that the tooltip
I have the following example code: <body> <div id=a></div> <div id=b></div> </body> If I
I have the following code example to use an object that receives the action
I have the following code example taken from the code of a Form :
I have the following code example for Visual C++ which creates an ActiveX object

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.