Adding custom items to stackpanel - c#

I'm working on a project (for Windows Phone 8 with Visual Studio 2012 with C#) where I want to display some items that each have:
a picture
a title
a description
to be able to be clicked (so that I can navigate to a certain Page)
So I thought I could do that with a stackpanel. But I'm not sure how I can add items that have the above properties and to be able to add those items from XAML. I tired adding items through a ItemsControl in stackpanel but I'm not sure how I can add more complex items like the one I want.

The best approach is to use a ListBox or LongListSelector rather than a StackPanel. You can then:
Data bind the list to the control itself, which will handle adding/deleting items from the control automatically
Define the view for each control using ListBox's ItemTemplate property
First of all, in your code-behind/ViewModel/what-have-you, you'll want to create an ObservableCollection of objects to display. ObservableCollection will let the control know to update in the case an item is added, removed, etc.
public ObservableCollection<T> foo = new ObservableCollection<T>();
In XAML, you'll then want to databind this ObservableCollection to the ListBox you've created:
<ListBox x:Name="ListBox" ItemsSource="{Binding foo}" />
Finally, you can define the ItemTemplate of the ListBox like so:
<ListBox x:Name="ListBox" ItemsSource="{Binding foo}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="2">
<TextBlock Text="{Binding Title}" />
<TextBlock Text="{Binding Description}" />
<Image Source="{Binding Image}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I'd highly recommend reading this guide, especially "Binding a control to a collection of objects" and the section after on DataTemplates. :)

Related

Controls in Listbox

I'm interesting, can I group some controls (image, 2-3 textboxes) in one element, and then push group of this elements in listbox? I'm trying to make a news reader of russian social network Vkontakte into Windwos Phone 7.
Each news has an image, text, and some other metadata. So I want to group all this info in one control and use it in listbox.
I tryied to push a grid(which had an image and two textboxes) into listbox, but it throws XamlParseException.
Also, I need to get the content of theese textboxes and images from code. In grid I can use
<Grid.Resources>
<src:Customers x:Key="customers"/>
</Grid.Resources>
Here is what you need:
A collection (ObservableCollection<T> recommended) of models (News in you case).
A ListBox
A DataTemplate
Example:
XAML:
<ListBox Name="ListBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
...
<Image Source="{Binding ImagePropertyInModel}" ... />
<TextBlock Text="{Binding TextPropertyInModel}" ... />
...
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code behind:
ListBox1.ItemsSource = <collection of models>;
Instead of the <Grid> in <DataTemplate> you can use a Custom Control (Templated Control) or a User Control that you may already have.
You can create an UserControl with the controls you want to use. With your ListBox you do something like:
<ListBox ItemsSource="{Binding Path=YourItemsSource}">
<ListBox.ItemTemplate>
<DataTemplate>
<my:MyUserControl DataContext="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
As ItemsSource of your ListBox you could use a ObservableCollection of News and through DataContext="{Binding}" you can bind to the News properties in your UserControl, e.g.:
<UserControl...>
<Image Source="{Binding Path=photoAttachment}"/>
</UserControl>

Binding expanders and listview to nested dictionary

I have a collection that looks like this:
ObservableDictionary<string, ObservableDictionary<string, SystemStatusItem>> ObservableColoServerStatus
I am working on creating a user control that I would like to bind as follows:
Each key (string) key from the outer dictionary should dynamically create an expander.
Inside that expander is a ListView/DataGrid/some control for displaying a collection of information
Each line in the ListView/DataGrid or whatever will be composed of a combination of the (string) Key and a property from the (object)Value of the inner dictionary.
Ideally it should look and operate similarly to a TreeView however Expanders have been widely used through our UI so I would like to stick to this for consistency.
I am new to WPF and have done a bit of basic data binding before but the nature of this nested relationship is making this really difficult to wrap my head around
Ok, first lets asume that you ObservableDictionary is a collection of items that have a Key property and a Value property. What you want can be done in wpf using data templates, please see this code, try do it in this way:
<ListBox ItemsSource="{Binding ObservableColoServerStatus}">
<ListBox.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Key}">
<ListBox ItemsSource="{Binding Value}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Key}"/>
<TextBlock Text="{Binding Value}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Expander>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
It is a list box, that has as ItemsSource your ObservableDictionary, then in the item template property you set the template that will use for representing all it itmes. In this case an expander, that have in the header the key a list box that take the elements from the Value of the first dictionary, and then you have set the template again. It is similar. Hope it works for you...

What WPF control can I use for a list of readonly items on WP7?

Right now I use a ListBox but that allows the user to select an item which I don't want. Is there a way to disable selecting or a more suitable control I can use?
Right now I have this:
<ListBox ItemsSource="{Binding Path=PersonNames}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontSize="20"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Use an ItemsControl, it's a base class without selection. (As it does not provide its own ScrollViewer you may need to add one (either in the template or around the control) if you need scrolling)
See the ListView control.

How to align the columns of an ItemsControl control (WPF)

I have defined a ItemsControl control in xaml as follows:
<ItemsControl BorderThickness="0" Name="SummaryList">
</ItemsControl>
In the same xaml file, I've defined the following DateTemplate for each item in the ItemsControl:
<DataTemplate DataType="{x:Type app:UpgradeItem}">
<StackPanel Orientation="Horizontal">
<Label Margin="5,5" Content="{Binding Path=ItemText,Mode=OneTime}" />
<Label Margin="5,5" Content="{Binding Path=FromVersion,Mode=OneTime}" />
<Label Margin="5,5" Content="{Binding Path=ToVersion,Mode=OneTime}" />
</StackPanel>
</DataTemplate>
I have bound the ItemsControl's ItemSource to a generlic list of UpgradeItems (i.e. List) and programmatically add to this list during execution. The items in the ItemsControl control populate correctly except that the data in the columns are not aligned. How can I modify this so that the three separate columns (ItemText, FromVersion and ToVersion) are aligned?
TIA
You should be able to use a SharedSize groups on grids, however I feel there is a better way to do this. Here's a link on the subject: http://blogs.microsoft.co.il/blogs/guy/archive/2009/06/30/wpf-grid-shared-size-groups.aspx
Part of what makes this tricky is that the ItemsControl doesn't have "columns": the control itself has no idea how its items' contents are laid out in relation to each other.
Your best bet would probably be to use one of the subclasses of ItemsControl that does support columnar data, such as ListView/GridView or the full-fledged DataGrid.

Modifying an individual TreeViewItem on a databound WPF TreeView without modifying the data source

I've got a MVVM WPF app with the TreeView databound to a viewmodel class. It is essentially a file explorer. I want to add the ability to "Add a new folder" to the hierarchy. To achieve the desired functionality what I am trying to do is simply switch the Textblock out for an editable TextBox in my datatemplate. This is what my datatemplate looks like:
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<StackPanel Name="tv_itempanel"
Orientation="Horizontal"
Margin="2">
<Image Source="{Binding Icon}" Margin="4"/>
<TextBlock Name="treeitem_tblock" Margin="4" Text="{Binding Name}"/>
<TextBox Width="200" Visibility="Collapsed" Name="treeitem_tbox"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
The problem is that I cannot modify an individual TreeViewItem since the treeview is databound. Any ideas? Thanks
Add a bool IsEditable property to your VM objects, and bind the visibility of the TextBox to is (using a converter to transform the boolean value to a Visibility enum). That way you don't need to manipulate the TreeViewItem directly, simply mark the data object as editable, and it will flow naturally to your view.

Categories

Resources