Expression >
Forums Home
>
Related Technologies Forums
>
Windows Presentation Foundation (WPF)
>
Regarding Treeview
Regarding Treeview
- 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
- 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.
Add the following lines to your FooViewModel class.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 } }
We can change the HierarchicalDataTemplate: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 use the Button class. any control implements ICommandSource can be used here.<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>
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- Marked As Answer byHua ChenMSFT, ModeratorMonday, November 09, 2009 2:07 AM
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
- Marked As Answer byHua ChenMSFT, ModeratorMonday, November 09, 2009 2:07 AM
All Replies
- 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.
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
- 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.
Add the following lines to your FooViewModel class.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 } }
We can change the HierarchicalDataTemplate: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 use the Button class. any control implements ICommandSource can be used here.<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>
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- Marked As Answer byHua ChenMSFT, ModeratorMonday, November 09, 2009 2:07 AM
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
RamHi 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
- Marked As Answer byHua ChenMSFT, ModeratorMonday, November 09, 2009 2:07 AM

