Add Event handler in Panel Resource : Resource « Windows Presentation Foundation « VB.Net






Add Event handler in Panel Resource

Add Event handler in Panel Resource
     
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="200" Width="300">
    <DockPanel LastChildFill="True">
        <DockPanel.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <EventSetter Event="Selected" Handler="TreeViewItem_Selected" />
            </Style>
        </DockPanel.Resources>
        <Button Click="Button_Click" DockPanel.Dock="Bottom" Content="Show Selected" MaxHeight="23" MaxWidth="100" />
        <TreeView FontSize="16" Name="tvTree">
            <TreeViewItem Header="A" IsExpanded="True">
                <TreeViewItem Header="1">
                    <TreeViewItem Header="2" />
                    <TreeViewItem Header="3" />
                </TreeViewItem>
                <TreeViewItem Header="B" IsExpanded="True">
                    <TreeViewItem Header="11" />
                    <TreeViewItem Header="22" />
                </TreeViewItem>
            </TreeViewItem>
            <TreeViewItem Header="C">
                <TreeViewItem Header="E">
                    <TreeViewItem Header="111" />
                    <TreeViewItem Header="222" />
                    <TreeViewItem Header="333" />
                </TreeViewItem>
                <TreeViewItem Header="F">
                    <TreeViewItem Header="1111" />
                    <TreeViewItem Header="2222" />
                    <TreeViewItem Header="333" />
                </TreeViewItem>
            </TreeViewItem>
        </TreeView>
    </DockPanel>
</Window>

//File:Window.xaml.vb
Imports System
Imports System.Windows
Imports System.Windows.Controls

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      InitializeComponent()
    End Sub
    Private Sub TreeViewItem_Selected(sender As Object, e As RoutedEventArgs)
      Dim item As TreeViewItem = TryCast(sender, TreeViewItem)
      If item Is e.OriginalSource Then
        Console.WriteLine(item.Header)
        Console.WriteLine(item.Items.Count)
      Else
        Console.WriteLine("Parent of selected")
        Console.WriteLine(item.Header)
        Console.WriteLine(item.Items.Count)
      End If
    End Sub

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
      Dim item As TreeViewItem = TryCast(tvTree.SelectedItem, TreeViewItem)

      If item IsNot Nothing Then
        MessageBox.Show("Item selected: " & Convert.ToString(item.Header), Title)
      Else
        MessageBox.Show("No item selected", Title)
      End If
    End Sub
  End Class
End Namespace

   
    
    
    
    
  








Related examples in the same category

1.Using a Drawing resourceUsing a Drawing resource
2.Create DrawingBrush based on GeometryDrawing ResourcesCreate DrawingBrush based on GeometryDrawing Resources
3.Using a Geometry resourceUsing a Geometry resource
4.Using a FrameworkElement resourceUsing a FrameworkElement resource
5.Disabling sharing for resourceDisabling sharing for resource
6.Referencing a Style resourceReferencing a Style resource
7.Populating a ResourceDictionary from XAMLPopulating a ResourceDictionary from XAML
8.Resources from markupResources from markup
9.Define a static ImageBrush resourceDefine a static ImageBrush resource
10.Use Resource to fill an EllipseUse Resource to fill an Ellipse
11.Use Resource to fill a ButtonUse Resource to fill a Button
12.Load resource from another file
13.Vertical reflected LinearGradientBrush static resourceVertical reflected LinearGradientBrush static resource
14.Create Reusable ShapesCreate Reusable Shapes
15.Reference a ResourceDictionary in a Different Assembly
16.Named ThemeNamed Theme
17.Using resources to define global stylesUsing resources to define global styles
18.EllipseGeometry as ResourceEllipseGeometry as Resource
19.Set Rectangle size with ResourcesSet Rectangle size with Resources
20.Find Control Styles with FindResource()Find Control Styles with FindResource()
21.Find Resource with FindResourceFind Resource with FindResource
22.TextGeometry as ResourceTextGeometry as Resource
23.Retrieving assembly manifest resources
24.Get Resource Names from AssemblyGet Resource Names from Assembly
25.Dynamic ResourceDynamic Resource
26.Localizable Application by putting localized resource in XamlLocalizable Application by putting localized resource in Xaml
27.Load Assembly ResourcesLoad Assembly Resources
28.BitmapImage as ResourcesBitmapImage as Resources
29.Cropped image as Resource
30.Load Xaml ResourceLoad Xaml Resource
31.Use Resources.Add to add static resouce from codeUse Resources.Add to add static resouce from code
32.Event Setter from ResourcesEvent Setter from Resources
33.Get resource in code as StoryboardGet resource in code as Storyboard