In a TreeView in WPF, is there a way to get the index of the child node that is selected and the index of the parent node of that selected child itemHow to get the name of the collection an item is bound to?How to implement IDataErrorInfo on string indexers databinding?TreeViewItem using User Control and bindingGetting a list item by indexGet the Parent node of a Child in WPF C# TreeViewItemsSource to property of SelectedItem of TreeViewWPF: TreeView get parent nodeAdding child node to parent in TreeView WPF?RadioButton grouping not working in WPF when using ItemControl and DataBindingGet the Parent node and Child node of selected items in WPF C# TreeView

Yet another calculator problem

How can faith be maintained in a world of living gods?

Template default argument loses its reference type

Could someone please explain what this inline #define assembly is doing?

Owner keeps cutting corners and poaching workers for his other company

Why did Tony's Arc Reactor do this?

After a few interviews, What should I do after told to wait?

Why does PAUSE key have a long make code and no break code?

If every star in the universe except the Sun were destroyed, would we die?

Protecting programs on external drives with the same priviledges as in Program Files

Why does 8 bit truecolor use only 2 bits for blue?

Bit floating sequence

Electric shock from pedals and guitar. Jacks too long?

Does the word voltage exist in academic engineering?

intensity color with custom ray tracing

What makes things real?

Is a MySQL database a viable alternative to LDAP?

Why does low tire pressure decrease fuel economy?

Is mountain bike good for long distances?

What makes an ending "happy"?

Live user not found in stage and dev

Entering the US with dual citizenship but US passport is long expired?

Was Robin Hood's point of view ethically sound?

What can we do about our 9-month-old putting fingers down his throat?



In a TreeView in WPF, is there a way to get the index of the child node that is selected and the index of the parent node of that selected child item


How to get the name of the collection an item is bound to?How to implement IDataErrorInfo on string indexers databinding?TreeViewItem using User Control and bindingGetting a list item by indexGet the Parent node of a Child in WPF C# TreeViewItemsSource to property of SelectedItem of TreeViewWPF: TreeView get parent nodeAdding child node to parent in TreeView WPF?RadioButton grouping not working in WPF when using ItemControl and DataBindingGet the Parent node and Child node of selected items in WPF C# TreeView






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















I am trying to populate a treeview using a three-level object.



Input for the treeview is ObservableCollection(GroupClass) where GroupClass contains a property called devices = ObservableCollection(DeviceDetails).



When I click on a DeviceDetails object in the UI, I would like to get the index of DeviceDetails item and the index of the GroupClass object it belongs to.



I have successfully populated the treeview using the collection of the GroupClass using a hierarchical template and data template.



I have looked at how to get the details of the selected item in a treeview, but most of the solution used TreeViewItem.Selected trigger.



In my case, as I am using a hierarchical template and data template I don't have any TreeView Item defined in my XAML.



Group Class:



public class GroupClass

public GroupClass()

this.devices = new ObservableCollection<DeviceDetails>();


public string groupName get; set;
public ObservableCollection<DeviceDetails> devices get; set;


public class DeviceDetails

public DeviceDetails()

this.status = "Online";
this.ipAddress = "192.168.1.1";
this.dateAccessed = DateTime.Now;


public string deviceName get; set;
public string status get; set;
public DateTime dateAccessed get; set;
public string ipAddress get; set;



XAML code for populating TreeView:



<Grid x:Name="ListPage" HorizontalAlignment="Stretch" Margin="0,80,0,0" >
<TreeView x:Name="HostTreeView" HorizontalAlignment="Stretch" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="x:Type local:GroupClass" ItemsSource="Binding devices">
<Grid HorizontalAlignment="Stretch" Name="HeaderPanel5">
<Grid Margin="0,0,0,0" >
<TextBlock Text="Binding groupName" />
</Grid>
</Grid>
</HierarchicalDataTemplate>
<DataTemplate DataType="x:Type local:DeviceDetails">
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="checkBox" />
<TextBlock Text="Binding deviceName" />
<TextBlock Text="Binding status" />
<TextBlock Text="Binding dateAccessed" />
<TextBlock Text="Binding ipAddress" />
<Button Content="Rename Device" Height="30"/>
<Button Content="Remove Device" Height="30"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>



I create a new object of type ObservableCollection and load the values into it from XML file. And then bind the created object to TreeView ItemsSource.



public static ObservableCollection<GroupClass> groups = new ObservableCollection<GroupClass>();
StreamReader sr = new StreamReader("IPProfileData");
XmlSerializer srlzr = new XmlSerializer(typeof(ObservableCollection<GroupClass>));
groups = (ObservableCollection<GroupClass>)srlzr.Deserialize(sr);
HostTreeView.ItemsSource = groups;


When I click on the Rename Device or Remove Device button, I would like to change the groups Object that is created earlier. Eventually my UI gets changed as TreeView is binded with groups object.










share|improve this question


























  • Do you really need the index of the item or just a reference to the item itself?

    – Flat Eric
    Mar 28 at 7:55











  • @FlatEric If it's possible to change/delete the selected item in the backend original object ObservableList<GroupClass> through that reference, then the reference works well for me.

    – Sudheer Kumar
    Mar 28 at 8:44












  • I would add a Command to the ViewModel and bind it in your button: Command="Binding DataContext.RenameCommand, RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type TreeView" CommandParameter="Binding"

    – Flat Eric
    Mar 28 at 8:52












  • @FlatEric I am not using MVVM model in my implementation :(

    – Sudheer Kumar
    Mar 28 at 8:57











  • You can bind to elements in your CodeBehind as well, you just need to set the DataContext, for example: DataContext="Binding RelativeSource=RelativeSource Self in your Window or UserControl

    – Flat Eric
    Mar 28 at 9:00

















4















I am trying to populate a treeview using a three-level object.



Input for the treeview is ObservableCollection(GroupClass) where GroupClass contains a property called devices = ObservableCollection(DeviceDetails).



When I click on a DeviceDetails object in the UI, I would like to get the index of DeviceDetails item and the index of the GroupClass object it belongs to.



I have successfully populated the treeview using the collection of the GroupClass using a hierarchical template and data template.



I have looked at how to get the details of the selected item in a treeview, but most of the solution used TreeViewItem.Selected trigger.



In my case, as I am using a hierarchical template and data template I don't have any TreeView Item defined in my XAML.



Group Class:



public class GroupClass

public GroupClass()

this.devices = new ObservableCollection<DeviceDetails>();


public string groupName get; set;
public ObservableCollection<DeviceDetails> devices get; set;


public class DeviceDetails

public DeviceDetails()

this.status = "Online";
this.ipAddress = "192.168.1.1";
this.dateAccessed = DateTime.Now;


public string deviceName get; set;
public string status get; set;
public DateTime dateAccessed get; set;
public string ipAddress get; set;



XAML code for populating TreeView:



<Grid x:Name="ListPage" HorizontalAlignment="Stretch" Margin="0,80,0,0" >
<TreeView x:Name="HostTreeView" HorizontalAlignment="Stretch" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="x:Type local:GroupClass" ItemsSource="Binding devices">
<Grid HorizontalAlignment="Stretch" Name="HeaderPanel5">
<Grid Margin="0,0,0,0" >
<TextBlock Text="Binding groupName" />
</Grid>
</Grid>
</HierarchicalDataTemplate>
<DataTemplate DataType="x:Type local:DeviceDetails">
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="checkBox" />
<TextBlock Text="Binding deviceName" />
<TextBlock Text="Binding status" />
<TextBlock Text="Binding dateAccessed" />
<TextBlock Text="Binding ipAddress" />
<Button Content="Rename Device" Height="30"/>
<Button Content="Remove Device" Height="30"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>



I create a new object of type ObservableCollection and load the values into it from XML file. And then bind the created object to TreeView ItemsSource.



public static ObservableCollection<GroupClass> groups = new ObservableCollection<GroupClass>();
StreamReader sr = new StreamReader("IPProfileData");
XmlSerializer srlzr = new XmlSerializer(typeof(ObservableCollection<GroupClass>));
groups = (ObservableCollection<GroupClass>)srlzr.Deserialize(sr);
HostTreeView.ItemsSource = groups;


When I click on the Rename Device or Remove Device button, I would like to change the groups Object that is created earlier. Eventually my UI gets changed as TreeView is binded with groups object.










share|improve this question


























  • Do you really need the index of the item or just a reference to the item itself?

    – Flat Eric
    Mar 28 at 7:55











  • @FlatEric If it's possible to change/delete the selected item in the backend original object ObservableList<GroupClass> through that reference, then the reference works well for me.

    – Sudheer Kumar
    Mar 28 at 8:44












  • I would add a Command to the ViewModel and bind it in your button: Command="Binding DataContext.RenameCommand, RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type TreeView" CommandParameter="Binding"

    – Flat Eric
    Mar 28 at 8:52












  • @FlatEric I am not using MVVM model in my implementation :(

    – Sudheer Kumar
    Mar 28 at 8:57











  • You can bind to elements in your CodeBehind as well, you just need to set the DataContext, for example: DataContext="Binding RelativeSource=RelativeSource Self in your Window or UserControl

    – Flat Eric
    Mar 28 at 9:00













4












4








4


1






I am trying to populate a treeview using a three-level object.



Input for the treeview is ObservableCollection(GroupClass) where GroupClass contains a property called devices = ObservableCollection(DeviceDetails).



When I click on a DeviceDetails object in the UI, I would like to get the index of DeviceDetails item and the index of the GroupClass object it belongs to.



I have successfully populated the treeview using the collection of the GroupClass using a hierarchical template and data template.



I have looked at how to get the details of the selected item in a treeview, but most of the solution used TreeViewItem.Selected trigger.



In my case, as I am using a hierarchical template and data template I don't have any TreeView Item defined in my XAML.



Group Class:



public class GroupClass

public GroupClass()

this.devices = new ObservableCollection<DeviceDetails>();


public string groupName get; set;
public ObservableCollection<DeviceDetails> devices get; set;


public class DeviceDetails

public DeviceDetails()

this.status = "Online";
this.ipAddress = "192.168.1.1";
this.dateAccessed = DateTime.Now;


public string deviceName get; set;
public string status get; set;
public DateTime dateAccessed get; set;
public string ipAddress get; set;



XAML code for populating TreeView:



<Grid x:Name="ListPage" HorizontalAlignment="Stretch" Margin="0,80,0,0" >
<TreeView x:Name="HostTreeView" HorizontalAlignment="Stretch" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="x:Type local:GroupClass" ItemsSource="Binding devices">
<Grid HorizontalAlignment="Stretch" Name="HeaderPanel5">
<Grid Margin="0,0,0,0" >
<TextBlock Text="Binding groupName" />
</Grid>
</Grid>
</HierarchicalDataTemplate>
<DataTemplate DataType="x:Type local:DeviceDetails">
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="checkBox" />
<TextBlock Text="Binding deviceName" />
<TextBlock Text="Binding status" />
<TextBlock Text="Binding dateAccessed" />
<TextBlock Text="Binding ipAddress" />
<Button Content="Rename Device" Height="30"/>
<Button Content="Remove Device" Height="30"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>



I create a new object of type ObservableCollection and load the values into it from XML file. And then bind the created object to TreeView ItemsSource.



public static ObservableCollection<GroupClass> groups = new ObservableCollection<GroupClass>();
StreamReader sr = new StreamReader("IPProfileData");
XmlSerializer srlzr = new XmlSerializer(typeof(ObservableCollection<GroupClass>));
groups = (ObservableCollection<GroupClass>)srlzr.Deserialize(sr);
HostTreeView.ItemsSource = groups;


When I click on the Rename Device or Remove Device button, I would like to change the groups Object that is created earlier. Eventually my UI gets changed as TreeView is binded with groups object.










share|improve this question
















I am trying to populate a treeview using a three-level object.



Input for the treeview is ObservableCollection(GroupClass) where GroupClass contains a property called devices = ObservableCollection(DeviceDetails).



When I click on a DeviceDetails object in the UI, I would like to get the index of DeviceDetails item and the index of the GroupClass object it belongs to.



I have successfully populated the treeview using the collection of the GroupClass using a hierarchical template and data template.



I have looked at how to get the details of the selected item in a treeview, but most of the solution used TreeViewItem.Selected trigger.



In my case, as I am using a hierarchical template and data template I don't have any TreeView Item defined in my XAML.



Group Class:



public class GroupClass

public GroupClass()

this.devices = new ObservableCollection<DeviceDetails>();


public string groupName get; set;
public ObservableCollection<DeviceDetails> devices get; set;


public class DeviceDetails

public DeviceDetails()

this.status = "Online";
this.ipAddress = "192.168.1.1";
this.dateAccessed = DateTime.Now;


public string deviceName get; set;
public string status get; set;
public DateTime dateAccessed get; set;
public string ipAddress get; set;



XAML code for populating TreeView:



<Grid x:Name="ListPage" HorizontalAlignment="Stretch" Margin="0,80,0,0" >
<TreeView x:Name="HostTreeView" HorizontalAlignment="Stretch" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="x:Type local:GroupClass" ItemsSource="Binding devices">
<Grid HorizontalAlignment="Stretch" Name="HeaderPanel5">
<Grid Margin="0,0,0,0" >
<TextBlock Text="Binding groupName" />
</Grid>
</Grid>
</HierarchicalDataTemplate>
<DataTemplate DataType="x:Type local:DeviceDetails">
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="checkBox" />
<TextBlock Text="Binding deviceName" />
<TextBlock Text="Binding status" />
<TextBlock Text="Binding dateAccessed" />
<TextBlock Text="Binding ipAddress" />
<Button Content="Rename Device" Height="30"/>
<Button Content="Remove Device" Height="30"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>



I create a new object of type ObservableCollection and load the values into it from XML file. And then bind the created object to TreeView ItemsSource.



public static ObservableCollection<GroupClass> groups = new ObservableCollection<GroupClass>();
StreamReader sr = new StreamReader("IPProfileData");
XmlSerializer srlzr = new XmlSerializer(typeof(ObservableCollection<GroupClass>));
groups = (ObservableCollection<GroupClass>)srlzr.Deserialize(sr);
HostTreeView.ItemsSource = groups;


When I click on the Rename Device or Remove Device button, I would like to change the groups Object that is created earlier. Eventually my UI gets changed as TreeView is binded with groups object.







c# wpf data-binding treeview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 27 at 11:59









Alex

2,6236 gold badges12 silver badges32 bronze badges




2,6236 gold badges12 silver badges32 bronze badges










asked Mar 28 at 6:22









Sudheer KumarSudheer Kumar

213 bronze badges




213 bronze badges















  • Do you really need the index of the item or just a reference to the item itself?

    – Flat Eric
    Mar 28 at 7:55











  • @FlatEric If it's possible to change/delete the selected item in the backend original object ObservableList<GroupClass> through that reference, then the reference works well for me.

    – Sudheer Kumar
    Mar 28 at 8:44












  • I would add a Command to the ViewModel and bind it in your button: Command="Binding DataContext.RenameCommand, RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type TreeView" CommandParameter="Binding"

    – Flat Eric
    Mar 28 at 8:52












  • @FlatEric I am not using MVVM model in my implementation :(

    – Sudheer Kumar
    Mar 28 at 8:57











  • You can bind to elements in your CodeBehind as well, you just need to set the DataContext, for example: DataContext="Binding RelativeSource=RelativeSource Self in your Window or UserControl

    – Flat Eric
    Mar 28 at 9:00

















  • Do you really need the index of the item or just a reference to the item itself?

    – Flat Eric
    Mar 28 at 7:55











  • @FlatEric If it's possible to change/delete the selected item in the backend original object ObservableList<GroupClass> through that reference, then the reference works well for me.

    – Sudheer Kumar
    Mar 28 at 8:44












  • I would add a Command to the ViewModel and bind it in your button: Command="Binding DataContext.RenameCommand, RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type TreeView" CommandParameter="Binding"

    – Flat Eric
    Mar 28 at 8:52












  • @FlatEric I am not using MVVM model in my implementation :(

    – Sudheer Kumar
    Mar 28 at 8:57











  • You can bind to elements in your CodeBehind as well, you just need to set the DataContext, for example: DataContext="Binding RelativeSource=RelativeSource Self in your Window or UserControl

    – Flat Eric
    Mar 28 at 9:00
















Do you really need the index of the item or just a reference to the item itself?

– Flat Eric
Mar 28 at 7:55





Do you really need the index of the item or just a reference to the item itself?

– Flat Eric
Mar 28 at 7:55













@FlatEric If it's possible to change/delete the selected item in the backend original object ObservableList<GroupClass> through that reference, then the reference works well for me.

– Sudheer Kumar
Mar 28 at 8:44






@FlatEric If it's possible to change/delete the selected item in the backend original object ObservableList<GroupClass> through that reference, then the reference works well for me.

– Sudheer Kumar
Mar 28 at 8:44














I would add a Command to the ViewModel and bind it in your button: Command="Binding DataContext.RenameCommand, RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type TreeView" CommandParameter="Binding"

– Flat Eric
Mar 28 at 8:52






I would add a Command to the ViewModel and bind it in your button: Command="Binding DataContext.RenameCommand, RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type TreeView" CommandParameter="Binding"

– Flat Eric
Mar 28 at 8:52














@FlatEric I am not using MVVM model in my implementation :(

– Sudheer Kumar
Mar 28 at 8:57





@FlatEric I am not using MVVM model in my implementation :(

– Sudheer Kumar
Mar 28 at 8:57













You can bind to elements in your CodeBehind as well, you just need to set the DataContext, for example: DataContext="Binding RelativeSource=RelativeSource Self in your Window or UserControl

– Flat Eric
Mar 28 at 9:00





You can bind to elements in your CodeBehind as well, you just need to set the DataContext, for example: DataContext="Binding RelativeSource=RelativeSource Self in your Window or UserControl

– Flat Eric
Mar 28 at 9:00












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391281%2fin-a-treeview-in-wpf-is-there-a-way-to-get-the-index-of-the-child-node-that-is%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391281%2fin-a-treeview-in-wpf-is-there-a-way-to-get-the-index-of-the-child-node-that-is%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현