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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:07:47+00:00 2026-05-12T12:07:47+00:00

I am having problems getting at a click event of a button and am

  • 0

I am having problems getting at a click event of a button and am using Silverlight 3.0 w/ matching Silverlight Toolkit.

Problem

I have this TreeView:

TreeView sample http://a.imagehost.org/0939/TreeView.png.

The value for a certain node is the sum of the values of its children. Only in leaves can data be added (for the time being).
What I want to achieve is that a user can add (and eventually remove) entries in the tree to eventually create a custom diagram.
To that end I would like the "plus sign" to insert a new line / node as child of the node where the user clicked. (I.e. if I click the plus at "Display", I get a line below to specify CRT or TFT or whatever.)

Thing is, for all my brain is worth, I don’t know how to receive any useful event.
The TextBlock, TextBox and Button are defined in a hierarchical template and I can’t define a Click-handler in that template.

OTOH, I haven’t found a way to get at the template items of a certain TreeViewItem from within (c#) code. Very well am I able to do trv.ItemContainerGenerator.GetContainerFromItem(item), and as Justin Angel showed I can very well change the font size, but didn’t find any way to access the textbox or button.

Is there any way to capture the click event to the button? Or any alternative way of getting something that gives the "add below" functionality?

Thank you in advance.


More Data

The treeview xaml is this:

<controls:TreeView x:Name="SankeyDataTree" 
    ItemTemplate="{StaticResource SankeyTreeTemplate}" BorderThickness="0" 
    Background="{x:Null}" HorizontalAlignment="Left" VerticalAlignment="Top">
    <controls:TreeViewItem IsExpanded="True">
        <controls:TreeViewItem.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="Loading..."/>
            </DataTemplate>
        </controls:TreeViewItem.HeaderTemplate>
    </controls:TreeViewItem>
</controls:TreeView>

I use this HierarchicalDataTemplate (and stole the appraoch from Timmy Kokke):

<Data:HierarchicalDataTemplate x:Key="SankeyTreeTemplate" ItemsSource="{Binding Children}">
    <Grid Height="24">
        <Grid.ColumnDefinitions>
            <!-- ... -->
        </Grid.ColumnDefinitions>
        <TextBlock Text="{Binding Path=Value.name, Mode=TwoWay}" VerticalAlignment="Center"/>
        <TextBox Text="{Binding Path=Value.flow, Mode=TwoWay}" Margin="4,0" VerticalAlignment="Center" d:LayoutOverrides="Width" Grid.Column="1" TextAlignment="Right" Visibility="{Binding Children, Converter={StaticResource BoxConverter}, ConverterParameter=\{box\}}"/>
        <TextBlock Text="{Binding Path=Value.throughput, Mode=TwoWay}" Margin="4,0" VerticalAlignment="Center" d:LayoutOverrides="Width" Grid.Column="1" TextAlignment="Right" Visibility="{Binding Children, Converter={StaticResource BoxConverter}, ConverterParameter=\{block\}}"/>
        <Button Margin="0" Grid.Column="2" Style="{StaticResource TreeViewItemButtonStyle}">
            <Image Source="../Assets/add.png" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Button>
    </Grid>
</Data:HierarchicalDataTemplate>

To this TreeView is bound a "SimpleTree", whose Values hold basically onle a string (name) and two doubles (flow and throughput).

public String name { get; set; }
public Double flow { get; set; }
public Double throughput { get; set; }

(Plus the code for the INotifyPropertyChanged to get a twoway bind to the text boxes.)

  • 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-12T12:07:48+00:00Added an answer on May 12, 2026 at 12:07 pm

    You can attach a Behavior to the Button in the HierarchicalDataTemplate and let that handle Click events from the Button.

    Download and install the Expression Blend 3 SDK. Add a reference to System.Windows.Interactivity in the project and add a Behavior attached to a Button:

    public class ButtonClickBehavior : Behavior<Button> {
    
      protected override void OnAttached() {
        base.OnAttached();
        AssociatedObject.Click += ButtonClick;
      }
    
      protected override void OnDetaching() {
        base.OnDetaching();
        AssociatedObject.Click -= ButtonClick;
      }
    
      void ButtonClick(object sender, RoutedEventArgs e) {
        Node node = AssociatedObject.DataContext as Node;
        if (node != null) {
          // Button clicked. Do something to associated node.
        }
      }
    
    }
    

    Attach the Behavior to the Button in the HierarchicalDataTemplate (assuming this namespace declaration: xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"):

    <Button Margin="0" Grid.Column="2" Style="{StaticResource TreeViewItemButtonStyle}">
      <Image Source="../Assets/add.png" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
      <interactivity:Interaction.Behaviors>
        <local:ButtonClickBehavior/>
      </interactivity:Interaction.Behaviors>
    </Button>
    

    If desired you can add properties to ButtonClickBehavior and set those from XAML to create a more reusable Behavior.

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

Sidebar

Related Questions

I am haveing problems getting the command event args following the second click using
Having a problem getting a TreeView control to display node images. The code below
I am having problems getting text within a table to appear centered in IE.
I am having problems getting my form to Post to my Save method in
I'm trying to merge two main menus together, but am having problems getting the
I'm having some problems getting my object to gracefully fail out if an invalid
I've been having tons of problems getting the non-xml configuration for Castle Windsor set
I bought a new Vista PC recently but was having lots of problems getting
I am having a problem getting a list of fields from a query defined
I'm having a problem getting access to a database which lives on a remote

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.