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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:55:32+00:00 2026-05-11T23:55:32+00:00

I have created a Treeview and used a stack panel to include a checkbox,

  • 0

I have created a Treeview and used a stack panel to include a checkbox, icon image and text for each node in the tree.
These nodes are created at runtime.
I also have a button object.
The xaml is below.

The problem i have is that, when the click me button is clicked, i need to traverse thru the tree view and if a checkbox is checked, perform some function.

Does anyone know how to find out if the checkbox for a node in the tree is checked, from the C# code behind ???

<Window x:Class="WPF_Explorer_Tree.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_Explorer_Tree" 
Title="KryptoG" Height="424" Width="815" Loaded="Window_Loaded">
<Window.Resources>
    <local:HeaderConverter x:Key="formatter" />
</Window.Resources>
<Grid>
    <TreeView x:Name="foldersItem" SelectedItemChanged="foldersItem_SelectedItemChanged" Background="#FFFFFFFF" BorderBrush="#FFFFFFFF" Foreground="#FFFFFFFF" Margin="0,0,236,112" AllowDrop="True" Visibility="Visible">
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel Name="ST" Orientation="Horizontal">
                                <CheckBox VerticalAlignment="Center"  Name="SelectedCheckBox" IsChecked="False"  Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
                            <Image Name="img"  Width="20"  Stretch="Fill" 
                                   Source="{Binding 
                                   RelativeSource={RelativeSource 
                                   Mode=FindAncestor, 
                                   AncestorType={x:Type TreeViewItem}}, 
                                   Path=Header, 
                                   Converter={x:Static local:HeaderToImageConverter.InstanceIcon}}"       
                                   />
                                <TextBlock VerticalAlignment="Center" Text="{Binding 
                                   RelativeSource={RelativeSource 
                                   Mode=FindAncestor, 
                                   AncestorType={x:Type TreeViewItem}}, 
                                   Path=Header, 
                                   Converter={StaticResource formatter}}" 
                                     />
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.Resources>
    </TreeView>
    <TreeView HorizontalAlignment="Right" Margin="0,0,12,12" Name="treeView1" Width="204" AllowDrop="True" BorderBrush="White" Foreground="White" />
    <Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,70" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click">Click Me</Button>
    <Button Height="23" HorizontalAlignment="Left" Margin="267,0,0,69" Name="button2" VerticalAlignment="Bottom" Width="75" Click="button2_Click">Click Me too</Button>
</Grid>

  • 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-11T23:55:32+00:00Added an answer on May 11, 2026 at 11:55 pm

    I would create a Two-Way data binding with that Check Box’s IsChecked property to a ViewModel object instead. Much easier than navigating the tree.


    Edit (per request of person asking):

    Here’s an example View Model (very simple that is only accounting for the IsChecked property):

    public class ViewModel : System.ComponentModel.INotifyPropertyChanged
    {
        private bool? _isChecekd;
        public bool? IsChecked
        {
            get { return _isChecekd; }
            set
            {
                if (_isChecekd != value)
                {
                    _isChecekd = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("IsChecked"));
                    }
                }
            }
        }
        #region INotifyPropertyChanged Members
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        #endregion
    }
    

    Now that you have an object implementing INotifyPropertyChanged, you can bind UI element properties to them. So you would update the IsChecked property of your CheckBox to this property. To do that, you first have to set the DataContext of Window1 in some way (or you could just do this on the TreeView itself as well). In your Window1.xaml.cs:

    public Window1()
            {
                InitializeComponent();
                this.DataContext = new ViewModel();
            }
    

    Then, in your Window1.xaml file, update the CheckBox IsChecked property:

    <CheckBox VerticalAlignment="Center"  Name="SelectedCheckBox" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"  Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
    

    And then, in whatever code you need to be able to interrogate the currently value of IsChecked, you can get to it this way (assuming this is Window1):

    ((ViewModel)this.DataContext).IsChecked
    

    Hope that helps!

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

Sidebar

Ask A Question

Stats

  • Questions 153k
  • Answers 153k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When do you want them to fire? CREATE TRIGGER AFTER… May 12, 2026 at 10:23 am
  • Editorial Team
    Editorial Team added an answer You have to lock the image and then work with… May 12, 2026 at 10:23 am
  • Editorial Team
    Editorial Team added an answer I'm not sure I understand why this doesn't work for… May 12, 2026 at 10:23 am

Related Questions

I have an application that reads a table from a database. I issue an
I have a TreeView setup so that each TreeViewItem has right-click context menu applied
I have an IEnumerable(of Employee) with a ParentID/ChildID relationship with itself that I can
Is there a way to add a resource to a ResourceDictionary from code without

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.