Silverlight: Defining an Event Handler in a Hierarchical Data Template

I'm having trouble clicking a button on a button, and I'm using the Silverlight 3.0 w / matching Silverlight Toolkit .

Problem

I have this TreeView:

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

The value for some node is the sum of the values ​​of its children. Only data can be added to the leaves (for now). I want to ensure that the user can add (and ultimately delete) entries in the tree in order to eventually create his own chart. For this, I would like the plus sign to insert a new line / node as a child of the node where the user clicked. (Ie, if I click the plus sign on “Display”, I will get the line below to indicate CRT or TFT or something else.)

The thing is, because my whole brain is standing, I do not know how to get any useful event. TextBlock, TextBox, and Button are defined in a hierarchical template, and I cannot define a Click handler in this template.

OTOH, I did not find a way to get the template elements of a specific TreeViewItem from inside (C #) code. Very good, I can do trv.ItemContainerGenerator.GetContainerFromItem (item), and as Justin Angel showed . I can change the font size very much, but could not find access to the text box or button.

Is there a way to capture a click event on a button? Or any alternative way to get something that gives the “add below” functionality?

Thanks in advance.


Extra data

TreeView xaml:

<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 Timmy Kokke 's score ):

<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>

This TreeView is bound with " SimpleTree ", the values ​​of which are stored mainly on a string (name) and two two-local ones (stream and bandwidth)).

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

( INotifyPropertyChanged, twoway .)

+3
2

Behavior Button HierarchicalDataTemplate Click Button.

Expression Blend 3 SDK. System.Windows.Interactivity Behavior, 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.
    }
  }

}

Behavior Button HierarchicalDataTemplate ( , : 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>

ButtonClickBehavior XAML, Behavior.

+3

. , Tag.

<Button Margin="0" Grid.Column="2" 
        Click="Button_Click" Tag="{Binding}"
        Style="{StaticResource     TreeViewItemButtonStyle}">            
  <Image Source="../Assets/add.png" Margin="0" 
         HorizontalAlignment="Center" VerticalAlignment="Center"/>        
</Button>

.

private void Button_Click(object sender, RoutedEventArgs e)
{
   var data = ((Button)sender).Tag as SimpleTreeNode
}

SimpleTreeNode - .

node .

+3

Source: https://habr.com/ru/post/1716575/


All Articles