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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:38:41+00:00 2026-05-24T17:38:41+00:00

In the WPF XAML code below, if I am in the SelectTaskItemClick event for

  • 0

In the WPF XAML code below, if I am in the SelectTaskItemClick event for the templated Button, how do I get the ListBoxItem ItemSource object that is currently selected?

    <!-- ListBox ITEMS -->
    <TaskDash:ListBoxWithAddRemove x:Name="listBoxItems" Grid.Row="1" Grid.Column="3" Grid.RowSpan="3"
        ItemsSource="{Binding}">
        <!--ItemsSource="{Binding}" DisplayMemberPath="Description">-->
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected" Value="{Binding Path=Selected}"/>
        </Style>
        <TaskDash:ListBoxWithAddRemove.ItemTemplate>
            <DataTemplate>
                <DockPanel>
                    <Button DockPanel.Dock="Left" Click="SelectTaskItemClick">SELECT</Button>
                    <TextBox DockPanel.Dock="Left" Name="EditableDescription" Text="{Binding Description}" Height="25" Width="100" />
                    <Button DockPanel.Dock="Left" Click="EditTaskItemClick">EDIT</Button>
                </DockPanel>
            </DataTemplate>
        </TaskDash:ListBoxWithAddRemove.ItemTemplate>
    </TaskDash:ListBoxWithAddRemove>

If I try to get the Parent or TemplateParent, it gives me the ContentPresenter or Style or something similar.

    private void SelectTaskItemClick(object sender, RoutedEventArgs e)
    {
        Button taskItemButton = (Button) e.OriginalSource;
        ContentPresenter taskItem = (ContentPresenter) taskItemButton.TemplatedParent;
        taskItem = (ContentPresenter)taskItemButton.TemplatedParent;
        Style taskItem2 = taskItem.TemplatedParent;
        taskItem2 = taskItem.TemplatedParent;
        DependencyObject taskItem3 = taskItem2.Parent;
        //DependencyObject taskItem3 = taskItem2.TemplatedParent;
        //TaskItem taskItemObj = taskItem2;
    }

In the code above, I’m guessing it is grabbing that from App.XAML where that custom ListBoxWithAddRemove control is defined. How do I traverse the actual form’s XAML instead [the first code shown above]?

<Style x:Key="{x:Type TaskDash:ListBoxWithAddRemove}" TargetType="{x:Type     TaskDash:ListBoxWithAddRemove}">
            <Setter Property="Margin" Value="3" />
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="MinWidth" Value="120"/>
            <Setter Property="MinHeight" Value="20"/>
            <Setter Property="AllowDrop" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TaskDash:ListBoxWithAddRemove}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="25" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <Button Grid.Column="0" Grid.Row="0" 
                                    Click="DeleteControlClick">Delete</Button>
                            <Button Grid.Column="1" Grid.Row="0" 
                                    Click="AddControlClick">Add</Button>
                            <Border 
                                Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"
                                  Name="Border" 
                                  Background="{StaticResource WindowBackgroundBrush}"
                                  BorderBrush="{StaticResource SolidBorderBrush}"
                                  BorderThickness="1"
                                  CornerRadius="2">
                                <ScrollViewer 
                                    Margin="0"
                                    Focusable="false">
                                    <StackPanel Margin="0" IsItemsHost="True" />
                                </ScrollViewer>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
  • 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-24T17:38:42+00:00Added an answer on May 24, 2026 at 5:38 pm

    You can use the VisualTreeHelper to walk up the tree and stop if you have an object of the right type, e.g.

    private void SelectTaskItemClick(object sender, RoutedEventArgs e)
    {
        var b = sender as Button;
        DependencyObject item = b;
        while (item is ListBoxItem == false)
        {
            item = VisualTreeHelper.GetParent(item);
        }
        var lbi = (ListBoxItem)item;
        //...
    }
    

    (If you just want to select the item that can (and should) just be done via the established binding, e.g.)

    private void SelectTaskItemClick(object sender, RoutedEventArgs e)
    {
        // The DataContext should be an item of your class that should
        // have a Selected property as you bind to it in a style.
        var data = (sender as FrameworkElement).DataContext as MyClass;
        data.Selected = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with the default Button in the xaml code below: <Window
Scenario: Currently I have this XAML code: <Button Content=_Cancel IsCancel=True Command={Binding Path=CancelCommand} Margin=5> <Button.ContentTemplate>
Platform: WPF, .NET 4, Visual Studio 2010 XAML code is as below: <telerik:RadTreeView x:Name=mytree
Do you use a code generator for WPF/XAML? If so, in what capacity? Is
Where to find Generic.xaml (or other code with the default look) for native WPF
I have a simple WPF (XAML) file that has some animated shapes and text.
The labels in the example below (WPF/XAML) just parade off the screen, no wrapping
I have the following xaml for changing the image for WPF button when mouse
I'm trying to implement the code below in my WPF project in order to
I have a list on my WPF xaml which contains two items. Below is

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.