How to assign value of the dataContext to ListBox control in silverlight? - silverlight

Hi I have contentControl in my user control. I am applying style to this contentControl which consist of TextBlock and ListBox. I am binding text of the textBlock to CategoryName(from Tag of the control). I want to bind category's child items to listBox. I have set ContentControl's dataContext property to child items[]. Now how to bind these child items to listbox which is in resource.
In loaded event of the user control
Panel pnl = sender as Panel;
Category category = panel.Tag as Category;
Items[] items = GetChildItemsByCategoryId(category.CategoryID);
mainContent.DataContext = items;

If the control is in the visual tree of mainContent, simply set in code or XAML
ItemsSource={Binding}

Related

Cannot clear selection of ListView

In my listview, when I select on of the item I will show a new view and set the listview to not select any item by set SelectedItem to be null. But the listview still select the old item that I selected. According to this link, I have set
IsSynchronizedWithCurrentItem="True"
But it still same. My item list is compose from the ItemViewModel that inherited from MvxViewModel
Are you using ListView(ListBox) or any custom control?
A class inherited from MS ListBox (such as ListView) has a static method to unselect all selected items:
ListBox.UnselectAll()
To unselect only one item you can cast selected item to ListBoxItem object and call:
ListBoxItem item = (ListBoxItem)obj;
item.IsSelected = false;
Have you tried it in your code behind? Or you want to achieve this declaratively by XAML markup?

retrieve usercontrol from itemscontrol item

i'm new to wpf and i am having a problem with items control. What i want to do is that i want to retrieve the user control that i've added in itemtemplate of items control. I tried using LoadContent() method of DataTemplate but it returns me the default template.
Here my code
ItemsControl parent = FindParent<ItemsControl>( this );
//this.isEditMode = true;
//this.editIngLayer.Visibility = Visibility.Visible;
foreach( var container in parent.Items )
{
DependencyObject contentPresenter=
parent.ItemContainerGenerator.ContainerFromItem( container ) as ContentPresenter;
//Something to retrieve the usercontrol
MyUserControl uC=contentPresenter.GetControl();
//
}
Thanks.
If you have your ItemsControl item with you then you can iterate its Visualtree to reach to your usercontrol using VisualTreeHelper
Recursive find child is explained in this post
How can I find WPF controls by name or type?

How Do I Change WPF Listview SelectedItem Font Color With ItemSource Bound?

I have WPF window containing a listview that has it's itemsource set to a collection of objects. When I access SelectedItem or SelectedItems[] or Items[], I get the my object back that's bound to that item, not the ListViewItem item itself. I have no idea how to select a row and change it's color since I can't access the item itself, like a winform listviewitem.
ListView derives from ItemsControl which exposes the ItemContainerGenerator property. This object allows you to map a bound entity to its ItemContainer (the item your are looking for) and back.

Accessing TextBoxes in WPF DataBound ListBox with foreach

I have a databound WPF ListBox with a custom itemtemplate -> datatemplate. Part of that template is also a ListBox.
On certain event I'd like to loop through all textboxes in the ListBox and retreive their values.
Is this possible?
You should be able to do this by using the ItemContainerGenerator and finding elements in the template:
foreach (var item in lb.Items)
{
var itemContainer = lb.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
// Name the TextBox in the template to find it here.
var textBox = itemContainer.ContentTemplate.FindName("?????", itemContainer) as TextBox;
var value = textBox.Text;
}
(If the TextBoxes you referred to are within the ListBox which is in the template you have to dig deeper by repeating the same method.)

Binding to an ObservableCollection of UserControls

Simple Silverlight question: I have an ObservableCollection<MyObject> in my viewmodel. Every MyObject has a Label property. If I bind a ListBox to the collection and set DisplayMemberPath to Label, or set the ItemTemplate to a TextBlock that binds the Text property to Label, all works as expected.
If I change MyObject so it derives from a UserControl, the Label text no longer shows up in the ListBox; each item just shows up as a blank strip a few pixels tall. Why is this? There's obviously something I'm missing here about how different things get rendered.
The ListBox determines that the set of items in its ItemsSource are already UIElement instances and therefore decides to use those elements directly as the content of the ListBoxItem elements it creates.

Resources