Answered by:
How to get the Item of a 0 based intex collection

Question
-
Dear all,
I have the follwoing code snipet that I try to use in order to retrive each items from a collection :
for (int i = 0, count = Items.Count; i < count; i++) { var ri = Items.GetItemAt(i) as RadialMenuItem; var angleDelta = 360.0 / count; var angleShift = ri.HalfShifted ? -angleDelta / 2 : 0; var startAngle = angleDelta * i + angleShift; var rotation = startAngle + angleDelta / 2; ri.AngleDelta = angleDelta; ri.StartAngle = startAngle; ri.Rotation = rotation; //Items[i].AngleDelta = angleDelta; //Items[i].StartAngle = startAngle; //Items[i].Rotation = rotation; }
What is wierd is that the Items.GetItemAt(i) is returning NULL and my collection is containing 2 items of type RadialMenuItem.
My Items collection is of type ItemCollection from my ItemsControl.Items
How can I retriev the item from a given index position ?
Any idea ?
Thanks for help
regards
Wednesday, November 18, 2015 5:52 PM
Answers
-
Are you totally 100% sure they are of type RadialMenuItem?
Because if I do:
ItemCollection it = new ListBox().Items; it.Add( new object()); it.Add(new object()); //foreach(var x in it) //{ // Debug.WriteLine("I got one"); //} for (int i = 0; i < it.Count; i++) { var x = it.GetItemAt(i) as object; if(x!= null) { Debug.WriteLine("I got one"); } }
Then I get two objects.
In other words.
If you cast to the right type then your code should give you 2 things.
Change your code to:
var ri = Items.GetItemAt(i);
With out the as...
Stick a breakpoint in on the line after.
See what you get out as ri.
Is it a RadialMenuItem which is uninitialized somehow, or is it something else?
.
Your for loop is a strange way to do it.
If it compiles, I can't see how that's your problem though.
Because if I do:
for (int i = 0, count = it.Count; i < count; i++) // for (int i = 0; i < it.Count; i++) { var x = it.GetItemAt(i) as object; if(x!= null) { Debug.WriteLine("I got one"); } }
Then that gets me my two objects OK.
I also wonder what Items is.
If you put a break point in before your for loop, when it breaks add a quickwatch on items and check that's what you think it's supposed to be.
- Edited by Andy ONeill Thursday, November 19, 2015 12:40 PM
- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, December 3, 2015 9:23 PM
Wednesday, November 18, 2015 6:42 PM -
>>How can I retriev the item from a given index position ?
>>Any idea ?
Use the indexer:
var ri = Items[i] as RadialMenuItem;
But your for loop looks a bit strange. You need to make sure that you are increasing the counter variable i as expected. Try this:
int count = Items.Count; for (int i = 0; i < count; i++) { var ri = Items[i] as RadialMenuItem; var angleDelta = 360.0 / count; var angleShift = ri.HalfShifted ? -angleDelta / 2 : 0; var startAngle = angleDelta * i + angleShift; var rotation = startAngle + angleDelta / 2; ri.AngleDelta = angleDelta; ri.StartAngle = startAngle; ri.Rotation = rotation; }
If ri is still NULL, the Items collection doesn't contain only RadialMenuItem objects.
Hope that helps.Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
- Proposed as answer by Xavier Xie-MSFT Friday, November 20, 2015 7:00 AM
- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, December 3, 2015 9:23 PM
Thursday, November 19, 2015 11:44 AM
All replies
-
Are you totally 100% sure they are of type RadialMenuItem?
Because if I do:
ItemCollection it = new ListBox().Items; it.Add( new object()); it.Add(new object()); //foreach(var x in it) //{ // Debug.WriteLine("I got one"); //} for (int i = 0; i < it.Count; i++) { var x = it.GetItemAt(i) as object; if(x!= null) { Debug.WriteLine("I got one"); } }
Then I get two objects.
In other words.
If you cast to the right type then your code should give you 2 things.
Change your code to:
var ri = Items.GetItemAt(i);
With out the as...
Stick a breakpoint in on the line after.
See what you get out as ri.
Is it a RadialMenuItem which is uninitialized somehow, or is it something else?
.
Your for loop is a strange way to do it.
If it compiles, I can't see how that's your problem though.
Because if I do:
for (int i = 0, count = it.Count; i < count; i++) // for (int i = 0; i < it.Count; i++) { var x = it.GetItemAt(i) as object; if(x!= null) { Debug.WriteLine("I got one"); } }
Then that gets me my two objects OK.
I also wonder what Items is.
If you put a break point in before your for loop, when it breaks add a quickwatch on items and check that's what you think it's supposed to be.
- Edited by Andy ONeill Thursday, November 19, 2015 12:40 PM
- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, December 3, 2015 9:23 PM
Wednesday, November 18, 2015 6:42 PM -
>>How can I retriev the item from a given index position ?
>>Any idea ?
Use the indexer:
var ri = Items[i] as RadialMenuItem;
But your for loop looks a bit strange. You need to make sure that you are increasing the counter variable i as expected. Try this:
int count = Items.Count; for (int i = 0; i < count; i++) { var ri = Items[i] as RadialMenuItem; var angleDelta = 360.0 / count; var angleShift = ri.HalfShifted ? -angleDelta / 2 : 0; var startAngle = angleDelta * i + angleShift; var rotation = startAngle + angleDelta / 2; ri.AngleDelta = angleDelta; ri.StartAngle = startAngle; ri.Rotation = rotation; }
If ri is still NULL, the Items collection doesn't contain only RadialMenuItem objects.
Hope that helps.Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
- Proposed as answer by Xavier Xie-MSFT Friday, November 20, 2015 7:00 AM
- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, December 3, 2015 9:23 PM
Thursday, November 19, 2015 11:44 AM