Click here to Skip to main content
15,885,957 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a control populated with data like this

Label1
---Field1
---Field2
---Field3
Label2
---Field4
---Field5
---Field6

i have two buttons Next and Back. Now Field1 is selected on clicking Next i have to show Field2 as current selected item. After Field3 if i click Next current selected item should be Field4. vice versa when i click Back.

My treeview structure is like this
XML
<TreeView x:Name="FieldTreeView" Grid.Row="1" BorderBrush="White" BorderThickness="0" ItemsSource="{Binding TreeDataSource}">
                            <TreeView.Resources>
                                <HierarchicalDataTemplate DataType="{x:Type self:Labels}" ItemsSource="{Binding FieldName}" >
                                    <TextBlock FontWeight="Bold" Text="{Binding Name}"/>
                                </HierarchicalDataTemplate>
                                <DataTemplate DataType="{x:Type self:Fields}">
                                    <TextBlock Text="{Binding Field}" />
                                </DataTemplate>
                            </TreeView.Resources>
                            <TreeView.ItemContainerStyle>
                                <Style TargetType="{x:Type TreeViewItem}">
                                    <Setter Property="IsExpanded" Value="True"/>
                                </Style>
                            </TreeView.ItemContainerStyle>
                        </TreeView>




My class structure

XML
public class Labels
{
    public Labels()
    {
        this.FieldName = new List<Fields>();
    }
    public string Name { get; set; }
    public List<Fields> FieldName { get; set; }
}
public class Fields
{
   public string Field { get; set; }
}



Populating my data like this
XML
List<Fields> fld = new List<Fields>();
            fld.Add(new Fields() { Field = "Field1" });
            fld.Add(new Fields() { Field = "Field2" });
            fld.Add(new Fields() { Field = "Field3" });

            List<Fields> fld1 = new List<Fields>();
            fld1.Add(new Fields() { Field = "Field4" });
      




      fld1.Add(new Fields() { Field = "Field5" });
            fld1.Add(new Fields() { Field = "Field6" });

            TreeDataSource.Add(new Labels() { Name = "Label1", FieldName = fld });
            TreeDataSource.Add(new Labels() { Name = "Label2", FieldName = fld1 });


Can any one please help me on this.
Posted
Updated 27-Oct-14 21:13pm
v2

1 solution

I'm not sure this will work but...

Bind the treeview's SelectedItem to an observable property (OnProperyChanged) called SelectedField. set the Mode to TwoWay.

In the button event you need to manipulate the fields collection to populate the SelectedField content to meet your requirements (select previous/next and traversing the parent collection when at the start/end of the field list).
 
Share this answer
 
Comments
M.Ravibalaji 28-Oct-14 5:01am    
Problem here is Treeview dose not a SelectedItem property in xaml. SelectedItem is readonly in codebehind.
Mycroft Holmes 28-Oct-14 6:47am    
Ah, I use the Telerik control library and it has the SelectedItem property in xaml. You will probably need to use some form of clickevent (node,item,etc)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900