Reputation: 1808
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))
Upvotes: 1
Views: 256
Reputation:
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).
Upvotes: 1