Wednesday, July 31, 2013

Set textblock text in a flipview in Metro App C#

Hi,


How can I set the text of a textblock which resides in a flipview, with value that I retrieved from the WCF service?


Here are some codes in the code-behind of ItemDetailPage:



protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
// Allow saved page state to override the initial item to display
if (pageState != null && pageState.ContainsKey("SelectedItem"))
{
navigationParameter = pageState["SelectedItem"];
}

// TODO: Create an appropriate data model for your problem domain to replace the sample data
var item = SampleDataSource.GetItem((String)navigationParameter);
this.DefaultViewModel["Group"] = item.Group;
this.DefaultViewModel["Items"] = item.Group.Items;
this.flipView.SelectedItem = item;

VirtualMachineDetails vmDet = new VirtualMachineDetails();
var selectedVm = item.Title;
if (selectedVm != "")
{
vmDet.VmName = selectedVm.ToString();
var myFlipView = flipView as FlipView;
var container = myFlipView.ItemContainerGenerator.ContainerFromItem(myFlipView.SelectedItem);
var children = AllChildren(container);

var vmStateTxtBox = children.OfType<TextBlock>().First(x => x.Name.Equals("tbStatus"));

vmStateTxtBox.Text = await objService.GetVMStatusAsync(vmDet);
}
}


public List<Control> AllChildren(DependencyObject parent)
{
var list = new List<Control> { };
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is Control)
{
list.Add(child as Control);
}
list.AddRange(AllChildren(child));
}
return list;
}



I get an error at the following line:



VisualTreeHelper.GetChildrenCount(parent)

Here's the error:



Can anyone point where I went wrong with the coding?


I referred to Jerry Nixon's article about accessing named control in a XAML in order to access the textblock in my flipview:


http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html


Thanks.


No comments:

Post a Comment