When the ActiveNode of the XamDataTree is set programmably, the previous active node is still highlighted, there's only a boarder around the new active node. Please see the screenshot below, the "Main2" node is previous selected node, I programmably change the ActiveNode to "Main10".
Here's the xaml:
<Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions><ig:XamDataTree ItemsSource="{Binding Items}" DisplayMemberPath="Caption" ActiveDataItem="{Binding SelectedItem, Mode=TwoWay}" x:Name="theTree">
<ig:XamDataTree.SelectionSettings> <ig:TreeSelectionSettings NodeSelection="Single"/> </ig:XamDataTree.SelectionSettings> <ig:XamDataTree.GlobalNodeLayouts> <ig:NodeLayout Key="ParentLayout" TargetTypeName="NavigationItem" DisplayMemberPath="Caption"/> </ig:XamDataTree.GlobalNodeLayouts> </ig:XamDataTree>
<Button Grid.Row="1" Click="Button_Click" >test</Button></Grid>
Here's the Code behind:
private void Button_Click(object sender, RoutedEventArgs e){ this.theTree.ActiveNode = this.theTree.Nodes[4];}
How to unselect the previous active node (make it not highlighted), and highlight the new active node?
Thanks.
Hello Ying,
XamDataTreeNodes have two highlighted states, Active and Selected. In the code in your button click event, it appears that you set the ActiveNode, but not the Selected one, so the selected one remains highlighted, while the Active one gets the highlighted border.
To set the SelectedNode on the XamDataTree after setting the Active node, I would recommend handling the XamDataTree’s ActiveNodeChanged event and setting a XamDataTreeNode object to e.NewActiveTreeNode in the handler. Then, set MyNode.IsSelected to true.
I have attached a sample application to demonstrate this.
Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewDeveloper Support Engineer IInfragistics Inc.www.infragistics.com/support
Just checking in, did you have any other questions or concerns on this matter?
Hi Andrew, thank you for your help on this matter. Your solution works.
However, we are trying to use MVVM, I am wondering how to set the ActiveNode in the viewmodel?
I changed your codes as the following:
XAML:
<Window x:Class="XamDataTreeActiveNodeCase.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ig="http://schemas.infragistics.com/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="30"/> </Grid.RowDefinitions> <Button x:Name="SetActiveNodeButton" Command="{Binding NewCommand}" Content="Set Active Node" Grid.Row="1"/>
<ig:XamDataTree x:Name="XamDataTree1" ItemsSource="{Binding Data}" DisplayMemberPath="Name" ActiveDataItem="{Binding SelectedItem, Mode=TwoWay}" ActiveNodeChanged="XamDataTree1_ActiveNodeChanged">
<ig:XamDataTree.GlobalNodeLayouts> <ig:NodeLayout Key="ParentLayout" TargetTypeName="SampleData" DisplayMemberPath="Name"/> </ig:XamDataTree.GlobalNodeLayouts> <ig:XamDataTree.SelectionSettings> <ig:TreeSelectionSettings NodeSelection="Single"/> </ig:XamDataTree.SelectionSettings>
</ig:XamDataTree>
</Grid></Window>
Code beghind:
public MainWindow() { InitializeComponent(); this.DataContext = new ViewModel();
private void XamDataTree1_ActiveNodeChanged(object sender, Infragistics.Controls.Menus.ActiveNodeChangedEventArgs e) { XamDataTreeNode XDTN = e.NewActiveTreeNode; XDTN.IsSelected = true; }
}
ViewModel:
using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Runtime.CompilerServices;using Microsoft.Practices.Prism.Commands;
namespace XamDataTreeActiveNodeCase{ public class ViewModel : INotifyPropertyChanged { string[] names = { "Andrew", "Alex", "Trish", "Valerie", "Jen", "Robert", "Steve", "Eric" };
public ObservableCollection<SampleData> Data { get; set; }
public ViewModel() { Data = new ObservableCollection<SampleData>(); for (int i = 0; i < names.Length; i++) { Data.Add(new SampleData() { Name = names[i], ID = i }); }
NewCommand = new DelegateCommand(AddNewDTO); }
private void AddNewDTO() { SelectedItem = new SampleData() { ID = 0, Name = "Andrew" }; }
private SampleData selectedItem; public SampleData SelectedItem { get { return selectedItem; } set { selectedItem = value; RaisePropertyChanged(); } }
public DelegateCommand NewCommand { get; set; }
#region INotifyPropertyChanged Implementation public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged([CallerMemberName] string caller = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(caller)); } } #endregion } public class SampleData { public string Name { get; set; } public int ID { get; set; } }}
When I set the SelectedItem in the viewmodel, the ActiveNodeChanged event is not firing.
Thank you.