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>
Related
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. :)
I have a list view that will contain notes that I input. I am having trouble figuring out how to have the list view item look how I want it to.
Below is how it should look:
And this is what it currently looks like:
How do I write the list view item in XAML so that the Date and time appear to the very top-right of each list view item, with the text of the note to the left?
<ListView x:Name="list" ItemsSource="{Binding Note}" BorderBrush="{x:Null}" BorderThickness="0">
<DataTemplate>
<ListViewItem>
<StackPanel Orientation="Horizontal">
</StackPanel>
</ListViewItem>
</DataTemplate>
</ListView>
Any help at all is much appreciated!
You are missing a number of elements required in order to get your screen to look the way you want.
You need to define the ItemTemplate for the ListView. You're on the right track here, it is a DataTemplate declared in XAML, you just have to apply it to the ListView.ItemTemplate property.
You need to set the HorizontalContentAlignment of the ListView to Stretch (the default is Left, which means your items will not fill the entire content area).
You need to use a DockPanel (or other similar panel) inside your DataTemplate to place your date content on the right, and the remainder of your content on the left.
You need to disable Horizontal Scrolling (ScrollViewer.HorizontalScrollBarVisbility) on the ListView in order to make your content wrap (otherwise it will just happily draw it all on one line).
I've included a sample ListView that should get you started.
<ListView
ItemsSource="{Binding Items}"
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock
TextWrapping="Wrap"
Text="{Binding Date}"
Background="Magenta"
DockPanel.Dock="Right" />
<TextBlock Text="{Binding Content}" Background="Lime" />
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
i like to customize my LongListSelector or ListBox in my own way so can any one help me to design it.. MY Code..
<StackPanel HorizontalAlignment="Left" Height="345" Margin="10,234,0,0" VerticalAlignment="Top" Width="413">
<phone:LongListSelector x:Name="list_organization" Height="340" Margin="10,0"/>
</StackPanel>
This is my code to bind the long list..
org = await client.searchOrganization(txtQuery.Text);
if (org != null)
{
var query = from c in org
select new { c.name,c.id,c.time,.. };
list_organization.ItemsSource = query.ToList();//bind the query to longlist
}
i want like this design page..
how to do it...?
First of all, remove explicit widths and heights. And if you only have a single LongListSelector inside the StackPanel, you can remove the StackPanel.
Individual items for the listbox-like containers is done by using the DataTemplate to replace ItemTemplate. Check the following MSDN link for more information: ListBox Styles and Templates.
Basically, here is how you do it:
<phone:LongListSelector x:Name="list_organization" Height="340" Margin="10,0"
ItemsSource="{Binding People}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<!-- your XAML for individual item goes here -->
<TextBlock Text="{Binding FirstName}" />
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
I try to create a listbox in my Windows Phone app. I tried to create a custom datatemplate for it. In a lot of sample I saw similar to this simple listbox:
<ListBox Width="350">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Test text 1" />
<TextBlock Text="Test text 2" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This is very simple isn't it? However the UI shows nothing. I can put textblocks and other control on the grid. I also tried using binding and itemssource but still nothing.
If I'm not using datatemplate just a simple textblock it shows the textblock:
<ListBox Width="350">
<TextBlock Text="haha" />
</ListBox>
Does anyone have any idea what do I wrong?
Thanks!
I think you need to bind the ItemSource of the listbox to something with items in it.
The second sample shows up because you are setting the content of the listbox directly.
What is the best solution to have something like a web_application DetaisView control(horizontal dataGrid with just one row!) with binding, insert and update features?
using horizontal datagrid
using a special listview
...
Create a user control that contains the specified fields you need to edit an item of your list
<UserControl x:Class="DisplayUserControl" ...>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name"/>
<TextBox Text="{Binding Name}"/>
</StackPanel>
...
</UserControl>
Bind the user control datacontext to the selected item of your List
<MainWindow>
<ListBox ItemsSource="{Binding to your data}" x:Name="list"/>
<local:DisplayUserControl DataContext="{Binding SelectedItem, ElementName=list}"/>
</MainWindow>
I just used a simple ListBox for displaying data