在TreeView WPF中切换导航 [英] Switch Navigation in TreeView WPF

查看:393
本文介绍了在TreeView WPF中切换导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控件填充了这样的数据



Label1

--- Field1

- --Field2

--- Field3

Label2

--- Field4

--- Field5

--- Field6



i有两个按钮Next和Back。现在单击下一步选择Field1我必须将Field2显示为当前所选项目。在Field3之后,如果我单击下一步当前所选项目应为Field4。反之亦然,当我点击Back。



我的树视图结构是这样的

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

<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

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

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.

推荐答案

我不确定这会有效但是......



将treeview的SelectedItem绑定到名为SelectedField的可观察属性(OnProperyChanged)。将模式设置为TwoWay。



在按钮事件中,您需要操作字段集合以填充SelectedField内容以满足您的要求(选择上一个/下一个并遍历在字段列表的开头/结尾处的父集合。)
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).


这篇关于在TreeView WPF中切换导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆