none
get hitted stack panel object from silverlight treeview.

    Question

  • hi,

    I have Silverlight TreeView with Stackpanel associated with each TreeViewItem.


    on this stack panel i have framework elements such as text block,check box etc.
    when I click on any one of the stack panel control, TreeViewItem Selected event gets called.
    but I can not get exactly control was hit?
    i referred following link:
    http://stackoverflow.com/questions/6475242/capture-click-on-treeview-item
    but in my case RoutedEventArgs e comes null.
    any ideas on this issue?


    - Shwetank

    Monday, March 05, 2012 8:34 AM

Answers

  • on this stack panel i have framework elements such as text block,check box etc.

    Hi I suggest you to use silverlight two way binding to get the value of silverlight listboxitem.

    please refer to the code below:

     

    <UserControl x:Class="SilverlightApplication212.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <sdk:TreeView Height="200" HorizontalAlignment="Left" Margin="10,10,0,0" Name="treeView1" VerticalAlignment="Top" Width="120" SelectedItemChanged="treeView1_SelectedItemChanged">
                <sdk:TreeView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding IsCheck, Mode=TwoWay}"></CheckBox>
                            <TextBlock Text="{Binding ItemValue, Mode=TwoWay}"/>
                        </StackPanel>
                    </DataTemplate>
                </sdk:TreeView.ItemTemplate>
            </sdk:TreeView>
        </Grid>
    </UserControl>
    

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    
    namespace SilverlightApplication212
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                List<ListItem> list = new List<ListItem>()
                { new ListItem(){ IsCheck=true, ItemValue="1"},
                    new ListItem(){ IsCheck=false, ItemValue="2"},
                    new ListItem(){ IsCheck=true, ItemValue="3"},
                    new ListItem(){ IsCheck=false, ItemValue="4"}};
                treeView1.ItemsSource = list;
            }
    
            private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
            {
                //breakpoint to see the value
            }
        }
        public class ListItem : INotifyPropertyChanged
        {
            private bool isCheck = false;
            private string itemValue = string.Empty;
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
            public bool IsCheck {
                get
                {
                    return this.isCheck;
                }
    
                set
                {
                    if (value != this.isCheck)
                    {
                        this.isCheck = value;
                        NotifyPropertyChanged("IsCheck");
                    }
                }
            }
            public string ItemValue
            {
                get
                {
                    return this.itemValue;
                }
    
                set
                {
                    if (value != this.itemValue)
                    {
                        this.itemValue = value;
                        NotifyPropertyChanged("ItemValue");
                    }
                }
            }
    
    
        }
    }
    

     

     Hope helpful

    Tuesday, March 06, 2012 11:20 PM