Ask a questionAsk a question
 

AnswerRegarding Treeview

  • Monday, November 02, 2009 5:31 AMRammohan Ammiti Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi All,


    I wrriten some code regarding tree view at dynamically add the Treeview item

    my proplem is if click the item text that should be navigated to different websites.. pls suggest me, here is my code.. pls suggest me

     download link: http://www.sendspace.com/file/g0pzws

    I know bit how nagigate the link thorugh hyperlinks.. but it's not suitable exactly..

     InitializeComponent();
                Run r = new Run("Text for hyperlink. ");
                Hyperlink h = new Hyperlink(r);
                h.NavigateUri = new Uri("http://google.com");
                h.Click += new RoutedEventHandler(h_Click);
                TextBlock tt = new TextBlock();
                tt.Inlines.Add(h);
                test.Children.Add(tt);


    Thank
    Ram

Answers

  • Tuesday, November 03, 2009 8:54 AMHua ChenMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hello Ram,

       Here is a way according to the MVVM model.

       We can bind the control (commad source) of TreeViewItem to the command property of the ViewModel.

       In your case the ViewModel is FooViewModel.

       Define a common command -- RelayCommand.

    namespace Clearway
    {
        /// <summary>
        /// A command whose sole purpose is to 
        /// relay its functionality to other
        /// objects by invoking delegates. The
        /// default return value for the CanExecute
        /// method is 'true'.
        /// </summary>
        public class RelayCommand : ICommand
        {
            #region Fields
    
            readonly Action<object> _execute;
            readonly Predicate<object> _canExecute;
    
            #endregion // Fields
    
            #region Constructors
    
            /// <summary>
            /// Creates a new command that can always execute.
            /// </summary>
            /// <param name="execute">The execution logic.</param>
            public RelayCommand(Action<object> execute)
                : this(execute, null)
            {
            }
    
            /// <summary>
            /// Creates a new command.
            /// </summary>
            /// <param name="execute">The execution logic.</param>
            /// <param name="canExecute">The execution status logic.</param>
            public RelayCommand(Action<object> execute, Predicate<object> canExecute)
            {
                if (execute == null)
                    throw new ArgumentNullException("execute");
    
                _execute = execute;
                _canExecute = canExecute;
            }
    
            #endregion // Constructors
    
            #region ICommand Members
    
            [DebuggerStepThrough]
            public bool CanExecute(object parameter)
            {
                return _canExecute == null ? true : _canExecute(parameter);
            }
    
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
    
            public void Execute(object parameter)
            {
                _execute(parameter);
            }
    
            #endregion // ICommand Members
        }
    }
    
     Add the following lines to your FooViewModel class.

    RelayCommand m_NavigateToLinkCommand;
    
            public ICommand NavigateToLinkCommand
            {
                get
                {
                    if (m_NavigateToLinkCommand == null)
                    {
                        m_NavigateToLinkCommand = new RelayCommand(
                              delegate(object Param)
                              {
                                  this.Navigate();
                              },
    
                            delegate(object Param)
                            {
                                return this.CanNavigate;
                            }
                          );
                    }
                    return m_NavigateToLinkCommand;
                }
            }
    
            private void Navigate()
            {
                System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", this.Name);
            }
    
            public bool CanNavigate
            {
                get
                {
                    return true;
                }
            }
    
     We can change the HierarchicalDataTemplate:

     <HierarchicalDataTemplate 
            x:Key="CheckBoxItemTemplate"
            ItemsSource="{Binding Children, Mode=OneTime}"
            >
            <StackPanel Orientation="Horizontal">
              <!-- These elements are bound to a FooViewModel object. -->
              <CheckBox
                Focusable="False" 
                IsChecked="{Binding IsChecked}" 
                VerticalAlignment="Center"
                />
           
                 <Button Command="{Binding NavigateToLinkCommand}">
                            <TextBlock Text="{Binding Name}">
                                
                            </TextBlock>
                  </Button>
    
            </StackPanel>
     </HierarchicalDataTemplate>
    
     We use the Button class. any control implements ICommandSource can be used here.
     
     Or we can change the ControlTemplate to give the button another appearance.

     If you have any question please let me know. 

     Thanks.
    Please mark the replies as answers if they help and unmark them if they provide no help
  • Wednesday, November 04, 2009 2:18 AMMin Chew Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Ram,

    Here's the implementation:http://www.sendspace.com/file/p5mjkj

    To keeps things straight forward, I've implemented the bare minimum (stripping out any checks, validations, etc).

    Hence, you might want to modify it to include any additional validations.

    Regards

All Replies

  • Monday, November 02, 2009 6:03 AMMin Chew Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Ram,

    if you're just trying to load up the default web browser with a particular link, you can invoke that using:

    System.Diagnostics.Process.Start(link);

    So basically, you don't even need to use a hyperlink object. Just tap into the event that you want, and call Process.Start();

    Hope that answers your question.
  • Tuesday, November 03, 2009 4:30 AMRammohan Ammiti Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Min,


      Thanks for ur reply..   pls download code from following link:  http://www.sendspace.com/file/g0pzws  and pls try to apply the navigation link.. pls 

    again Thanks
    Ram


     
  • Tuesday, November 03, 2009 8:54 AMHua ChenMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hello Ram,

       Here is a way according to the MVVM model.

       We can bind the control (commad source) of TreeViewItem to the command property of the ViewModel.

       In your case the ViewModel is FooViewModel.

       Define a common command -- RelayCommand.

    namespace Clearway
    {
        /// <summary>
        /// A command whose sole purpose is to 
        /// relay its functionality to other
        /// objects by invoking delegates. The
        /// default return value for the CanExecute
        /// method is 'true'.
        /// </summary>
        public class RelayCommand : ICommand
        {
            #region Fields
    
            readonly Action<object> _execute;
            readonly Predicate<object> _canExecute;
    
            #endregion // Fields
    
            #region Constructors
    
            /// <summary>
            /// Creates a new command that can always execute.
            /// </summary>
            /// <param name="execute">The execution logic.</param>
            public RelayCommand(Action<object> execute)
                : this(execute, null)
            {
            }
    
            /// <summary>
            /// Creates a new command.
            /// </summary>
            /// <param name="execute">The execution logic.</param>
            /// <param name="canExecute">The execution status logic.</param>
            public RelayCommand(Action<object> execute, Predicate<object> canExecute)
            {
                if (execute == null)
                    throw new ArgumentNullException("execute");
    
                _execute = execute;
                _canExecute = canExecute;
            }
    
            #endregion // Constructors
    
            #region ICommand Members
    
            [DebuggerStepThrough]
            public bool CanExecute(object parameter)
            {
                return _canExecute == null ? true : _canExecute(parameter);
            }
    
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
    
            public void Execute(object parameter)
            {
                _execute(parameter);
            }
    
            #endregion // ICommand Members
        }
    }
    
     Add the following lines to your FooViewModel class.

    RelayCommand m_NavigateToLinkCommand;
    
            public ICommand NavigateToLinkCommand
            {
                get
                {
                    if (m_NavigateToLinkCommand == null)
                    {
                        m_NavigateToLinkCommand = new RelayCommand(
                              delegate(object Param)
                              {
                                  this.Navigate();
                              },
    
                            delegate(object Param)
                            {
                                return this.CanNavigate;
                            }
                          );
                    }
                    return m_NavigateToLinkCommand;
                }
            }
    
            private void Navigate()
            {
                System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", this.Name);
            }
    
            public bool CanNavigate
            {
                get
                {
                    return true;
                }
            }
    
     We can change the HierarchicalDataTemplate:

     <HierarchicalDataTemplate 
            x:Key="CheckBoxItemTemplate"
            ItemsSource="{Binding Children, Mode=OneTime}"
            >
            <StackPanel Orientation="Horizontal">
              <!-- These elements are bound to a FooViewModel object. -->
              <CheckBox
                Focusable="False" 
                IsChecked="{Binding IsChecked}" 
                VerticalAlignment="Center"
                />
           
                 <Button Command="{Binding NavigateToLinkCommand}">
                            <TextBlock Text="{Binding Name}">
                                
                            </TextBlock>
                  </Button>
    
            </StackPanel>
     </HierarchicalDataTemplate>
    
     We use the Button class. any control implements ICommandSource can be used here.
     
     Or we can change the ControlTemplate to give the button another appearance.

     If you have any question please let me know. 

     Thanks.
    Please mark the replies as answers if they help and unmark them if they provide no help
  • Tuesday, November 03, 2009 10:55 AMRammohan Ammiti Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     


    Hi Hua,


     Pls send me the code of that.. my side it's not working properly.. pls edit my code and then send to me.. really thanks to u

    Again Thanks
    Ram
  • Wednesday, November 04, 2009 2:18 AMMin Chew Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Ram,

    Here's the implementation:http://www.sendspace.com/file/p5mjkj

    To keeps things straight forward, I've implemented the bare minimum (stripping out any checks, validations, etc).

    Hence, you might want to modify it to include any additional validations.

    Regards