如何根据选择TabItem的关闭它的头球 [英] How to select a TabItem based off of it's Header

查看:108
本文介绍了如何根据选择TabItem的关闭它的头球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的节目,我有一个 TabItem的是被选中当树型视图用等效的被选中。



这是我目前有(它的工作原理):

  (parent_TreeViewItem.Items.Contains(的SelectedItem))
{
根据树视图
.OfType< tabControl1.SelectedItem = tabControl1.Items //更改选项卡; TabItem的方式>()的SingleOrDefault(N => ; n.Header.ToString()== SelectedItem.Header.ToString());
}



与我在做什么这一次不同的是,在 TabItem的,我是选择由<​​code>字符串 。和整数



例如:在树型视图选择将永远有一个名为到达,而 TabItem的将有像到达+整数。在整数价值将来自父节点的



有关这个过程中,我想,我需要先获得报头中的父节点值,因为它包含了整数价值,我需要。然后,我将需要修改上述好歹我的代码来查询与头节点一样,到达+ parentHeader



我怎么会做这样的事?



感谢您。



更新



我当前的代码,使用@ varocarbas的回答。我使用所涉及的整数 curNumber 设置为父母的<$ c的值回答的第一个版本$ C>头。代码编译,但不会做时,点击了到达节点什么。

 如果(parent_TreeViewItem.Items.Contains( SelectedItem.Parent))//位置 - 动作片
{
树型视图parentItem =(树型视图)SelectedItem.Parent;
INT curNumber = getNumber(parentItem.Header.ToString());

tabControl1.SelectedItem = tabControl1.Items //更改根据树视图
.OfType&所述标签; TabItem的方式>()的SingleOrDefault(N => n.Header.ToString()==的SelectedItem .Header.ToString()+ curNumber.ToString());
}

公共静态INT getNumber(字符串parentNodeHeader)
{
INT curNumber = 0;
curNumber = Convert.ToInt32(parentNodeHeader);

返回curNumber;
}



更新2:由于到达节点为我所用,我已经改变了如果语句在我的第一行父节点的孙子:

 如果(parent_TreeViewItem.Items.Contains(SelectedItem.Parent))


解决方案

首先,你必须得到父节点,并包含在其头数:

 树型视图parentItem =(树型视图)selectedItem.Parent; 
INT curNumber = getNumber(parentItem.Header.ToString());



getNumber 是检索数的函数从在父节点头它的准确位置。你要告诉更多的,为了写一个适当的功能;暂且,只是基础知识(它提取输入字符串的所有号码):

 私人诠释getNumber(字符串parentNodeHeader )
{
INT curNumber = 0;

//必需的字符串,分析动作
//样的功能:提取所有的数字在给定的字符串
串outString =;
诠释计数= -1;

{
计数=计数+ 1;
字符curChar = Convert.ToChar(parentNodeHeader.Substring(计数,1));
如果(Char.IsNumber(curChar))
{
outString = outString + parentNodeHeader.Substring(计数,1);
}
},而(COUNT< parentNodeHeader.Length - 1);

如果(outString =!)
{
curNumber = Convert.ToInt32(outString);
}

返回curNumber;
}



然后你必须更新查询,以说明新信息:

  .OfType< TabItem的方式>()的SingleOrDefault(N => n.Header.ToString()== selectedItem.Header。的ToString()+ curNumber.ToString()); 



更​​新



以上只是显示功能什么样的代码,我通常依靠;但对于简单的情况下(如获得字符串中的所有数字的建议之一),你可能更喜欢依靠正则表达式,由薇薇的建议。你可能会依赖于线的东西:

 私人诠释getNumber(字符串parentNodeHeader)
{
System.Text.RegularExpressions.Match M = System.Text.RegularExpressions.Regex.Match(parentNodeHeader,@\d +);
返回Convert.ToInt32(m.Value);
}

这功能只提供了第一套发现顺序编号;不同的结果除上述功能,但足以作为概念(这个答案的意图)的证明。


In my program I have a tabItem that gets selected when a TreeViewItem with an equivalent header is selected.

This is what I currently have (It works):

(parent_TreeViewItem.Items.Contains(SelectedItem))
{
          tabControl1.SelectedItem = tabControl1.Items //Changes tab according to TreeView
                .OfType<TabItem>().SingleOrDefault(n => n.Header.ToString() == SelectedItem.Header.ToString());
}

The difference with what I'm doing this time is that the tabItem's header that I'm selecting is composed of a string and an integer.

For example: The TreeViewItem selected will always have a header named "Arrival", but the tabItem's header will have a form like "Arrival" + integer. The integer value will come from the parent node's header.

For this process I'm thinking that I'll first need to get the header value of the parent node, since it contains that integer value I need. Then I'll need to modify my code above in someway to query for a node with a header like, "Arrival" + parentHeader.

How would I do something like this?

Thank you.

UPDATE

My current code, using @varocarbas's answer. I am using the first version of the answer that involved setting the integer curNumber to the value of the parent's header. The code compiles but does not do anything when the "Arrival" node is clicked on.

if (parent_TreeViewItem.Items.Contains(SelectedItem.Parent)) //Location - Actions tabs
{
       TreeViewItem parentItem = (TreeViewItem)SelectedItem.Parent;
       int curNumber = getNumber(parentItem.Header.ToString());

       tabControl1.SelectedItem = tabControl1.Items //Changes tab according to TreeView
             .OfType<TabItem>().SingleOrDefault(n => n.Header.ToString() == SelectedItem.Header.ToString() + curNumber.ToString());
}

public static int getNumber(string parentNodeHeader)
{
    int curNumber = 0;
    curNumber = Convert.ToInt32(parentNodeHeader);

    return curNumber;
}

UPDATE 2: Because the "Arrival" node is the grandchild of the node I was using as a parent I have changed the if statement in my first line to:

 if (parent_TreeViewItem.Items.Contains(SelectedItem.Parent))

解决方案

Firstly, you have to get the parent node and the number contained in its header:

TreeViewItem parentItem = (TreeViewItem)selectedItem.Parent;
int curNumber = getNumber(parentItem.Header.ToString());

getNumber is a function to retrieve the number from its exact location in the parent node header. You have to tell more about that in order to write a proper function; for the time being, just the basics (it extracts all the numbers in the input string):

private int getNumber(string parentNodeHeader)
{
    int curNumber = 0;

    //Required string-analysis actions
    //Sample functionality: extract all the numbers in the given string
    string outString = "";
    int count = -1;
    do
    {
        count = count + 1;
        Char curChar = Convert.ToChar(parentNodeHeader.Substring(count, 1));
        if (Char.IsNumber(curChar))
        {
            outString = outString + parentNodeHeader.Substring(count, 1);
        }
    } while (count < parentNodeHeader.Length - 1);

    if (outString != "")
    {
        curNumber = Convert.ToInt32(outString);
    }

    return curNumber;
}

And then you have to update the query to account for the new information:

 .OfType<TabItem>().SingleOrDefault(n => n.Header.ToString() == selectedItem.Header.ToString() + curNumber.ToString());

UPDATE

The function above just shows the kind of code I usually rely on; but for simple situations (like the proposed one of getting all the numbers in a string), you might prefer to rely on Regex, as suggested by Viv. You might rely on something on the lines of:

private int getNumber(string parentNodeHeader)
{
    System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(parentNodeHeader, @"\d+");
    return Convert.ToInt32(m.Value);
}

This function only delivers the first set of consecutive numbers it finds; different result than the function above but enough as a proof of concept (intention of this answer).

这篇关于如何根据选择TabItem的关闭它的头球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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