I am new to C#/XAML development and I have just started creating very basic windows phone 8 application. I want to get all the elements (Controls) added in the stackpanel as child to the stackpanel using stackpanel.Children.Add(name_of_the_control).
Now I want to get all the elements or controls added in the stackpanel but do not know how to do it. I have searched on the net but could not get helpful link.
Please help me with the process of how to achieve it.
You can access all controls added to StackPanel from it's Children property, for example :
foreach(var control in stackpanel.Children)
{
//here you can get each element of stackpanel in control variable
//further example :
if(control is RadioButton)
{
var radio = (RadioButton)control;
//do something with RadioButton
}
else if(control is TextBlock)
{
var textblok = (TextBlock)control;
//do something with TextBlock
}
//add other possible type of control you want to manipulate
}
Related
In UWP, I want to handle and skip the tab key navigation for particular set of controls dynamically.
For e.g., I've two user controls (Both have lot of children which will be added dynamically) in my main page and want to skip tab key navigation for one usercontrol dynamically on specific scenario for a moment.
So I've tried to set "IsTabStop" as false to that UserControl. But which is not effective on its child controls. Still tab key focus moved inside the children of that UserControl.
Note: If I set "IsEnabled" as false, then its working. But I don't want to use because it affects Visual appearance.
Thanks in advance.
There is no IsTabStop property in StackPanel class. The IsTabStop property is defined in Windows.UI.Xaml.Controls.Control class, and the StackPanel class is not inherited from Control class, either directly or indirectly. You can not use IsTabStop property to set a StackPanel instance.
Therefore, if you want to skip tab key navigation for one stackpanel, you need to set the IsTabStop property to False in each control in the stackpanel.
Update:
By testing, the child elements in a UserControl can not inherit the value of IsTabStop property. Therefore, you cannot skip tab key navigation for all the child elements in a UserControl by setting the IsTabStop property to False.
You could use a method defind in your UserControl class to set IsTabStop to false for every item in your UserControl.
For example:
MyUserControl.cs
public void SetIsTabStop(bool flag)
{
var result = VisualTreeFindAll<Control>(this);
foreach (var item in result)
{
item.IsTabStop=flag;
}
}
private IList<T> VisualTreeFindAll<T>(DependencyObject element)
where T : DependencyObject
{
List<T> retValues = new List<T>();
var childrenCount = VisualTreeHelper.GetChildrenCount(element);
for (var i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(element, i);
var type = child as T;
if (type != null)
{
retValues.Add(type);
}
retValues.AddRange(VisualTreeFindAll<T>(child));
}
return retValues;
}
Use the name of a UserControl instance to call the SetIsTabStop(bool flag) method with False.
userControlName.SetIsTabStop(false);
Update:
Maybe you could try the following workaround.
Add the KeyDown event handler for the UserControl in your Page.
private void UserControl_KeyDown (object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Tab)
{
e.Handled = true;
button1.Focus(FocusState.Keyboard);
}
}
You need to let the first control except the UserControl and all its child controls get the focus, then the key navigation will skip the child controls in the UserControl except the first child control in the UserControl.
how to check if an component is already in the layout?
I have an 4x4 gridlayout
with some buttons
one button is to show an datagrid
one to show inputfields
what I try to do is
MenuBar.Command tablecommand = new MenuBar.Command() {
public void menuSelected(MenuItem selectedItem) {
output.setValue("clean components");
layout2.removeComponent(name);
layout2.removeComponent(name2);
layout2.removeComponent(button);
layout2.removeComponent(cp);
//layout2.removeComponent(grid);
//if layout2 !contains grid
layout2.addComponent(grid,1,2);
}
};
without check I get an exception
java.lang.IllegalArgumentException: Component is already in the container
at com.vaadin.ui.GridLayout.addComponent
when I click on the showgrid button the second time
the only thing I can find is ".equals(obj)"
You can look if the component has a parent.
If yes, then it's already in place somewhere.
https://vaadin.com/api/com/vaadin/ui/Component.html#getParent--
Once a VariableSizedWrapGrid has rendered its content, is there a way to refresh its layout so that it re-renders and re-applies the sizes if they have changed?
For example, the first item is twice the size of the rest, then I change the column and row span values in the view model. Now I want to invoke the grid to re-render.
Here's a simple way:
public void Update(GridView gridView)
{
if (!(gridView.ItemsPanelRoot is VariableSizedWrapGrid))
throw new ArgumentException("ItemsPanel is not VariableSizedWrapGrid");
foreach (var container in gridView.ItemsPanelRoot.Children.Cast<GridViewItem>())
{
var data = container.Content as Common.ModelBase;
VariableSizedWrapGrid.SetRowSpan(container, data.RowSpan);
VariableSizedWrapGrid.SetColumnSpan(container, data.ColSpan);
}
gridView.ItemsPanelRoot.InvalidateMeasure();
}
Assuming Windows.UI.Xaml.Controls.GridView as ItemsControl
Assuming Windows.UI.Xaml.Controls.VariableSizedWrapGrid as ItemPanel
I integrated it into the custom grid like this: http://codepaste.net/aopvks
Best of luck!
I'm developing a windows store application with C# and XAML. I am using a ListView to display the collection of data.
Inside the ListView I have a data template which has grids and a TextBlock in the grid. I want to tap/click the TextBlock and give action without selecting the parent ListView item as I already have event to handle the selected ListView item. I don't want both to overlap.
Thanks in advance for any response.
So you want to be able to select the ListViewItem when tapping one part of it, but not the TextBlock? If this is the case, in the TextBlock's Tapped event add e.Handled = true;. This should make it so that it isn't further routed up to the parent ListView.
The other thing you can do (which will likely be a more general solution to whatever you want to do with your ListViewItems) is to not use thing SelectionChanged event and instead handle everything with ItemClick. You can then deduce whether the OriginalSource of the event is indeed your TextBlock. Then, if it's not the TextBlock, change the parent ListView's SelectedItem.
An example for chceking the OriginalSource
public static void ItemClickEvent(object sender, ItemClickEventArgs e)
{
if(e.OriginalSource is TextBlock)
DoNothingOrMaybeTextBlockEvent();
else
{
ListView.SelectedItem = e.ClickedItem;
}
}
Hope this helps.
Edit: Added some example code for the OriginalSource check
i am developing an c#/xaml metro application .
I am having a listbox , in which depending upon on one particular condition i want to change only one listboxitem background colour .
This is how i have tried :-
if (lststudents != null)
{
foreach (StudentClass obj in lststudents)
{
if (obj.stutype == "Red House")
{
///I am unable to typecast to listboxitem, since it is of type student class
ListBoxItem lstbxbitem = (obj)ListBoxItem;
///
lstbxbitem.Background = new SolidColorBrush(Colors.Red);
}
}
}
lstbxbStudents.ItemsSource = lststudents ;
Please Note :- i want to change background colour of only some listboxitems depending upon some particular condition.
Please let me know how i can achieve this ??
What are the various alternatives ??
Thanks in advance.
Use DataTemplate in your ListBox, then you can create a DataTemplateSelector to style the specific item in you ListBox.
You can refer to these links:
ItemsControl.ItemTemplate property
ItemsControl.ItemTemplateSelector property
Here's a good article:
Using Dynamic XAML in Windows 8 Store Apps