I'm writing simple WPF Application and I wanted to use ListView to display List of items. My code is:
WPF.xaml
<ListView Grid.Column="0" Grid.Row="1" Margin="10,0,10,5" ItemsSource="{Binding MyCollection.Elements}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ElementDescriptions}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
WPF.xaml.cs
public MyViewModel ViewModel
{
get { return DataContext; }
set { DataContext = value; }
}
MyViewModel.cs
public OwnedCollection Elements { get; set; }
OwnedCollection.cs
public List<ElementDescriptions> ElementDescriptions { get; set; }
I'm 100% sure, that communication between View and ViewModel is correct, because displaying simple message doesn't make me troubles. Am I doing right binding in ListView?
A couple things:
First,
TextBlock Text="{Binding ElementDescriptions}"
doesn't make a lot of sense because ElementDescriptions is a collection. If you want to loop through all the ElementDescriptions in your List you should really be binding the ItemSource of the ListView to ElementDescriptions then accessing some text property of the ElementDescriptions class:
<ListView Grid.Column="0" Grid.Row="1" Margin="10,0,10,5" ItemsSource="{Binding MyCollection.ElementsElementDescriptions }">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ElementDescriptions.SomeTextField}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Second, are you using INotifyPropertyChanged so the view knows to update? More info on that here: OnPropertyChanged with a List
Related
I have a little problem and I don't know how to fix it.
In my View I have a combobox, that should be able to display the property "Name" of the ObservableCollection "Phases". I already tried to show a single "Name-property" without a Datatemplate and it worked. I think I messed something up with the binding in the DataTemplate. What is wrong? Can you help me?
Here is my View:
<ComboBox ItemsSource="{Binding Phases}"
SelectedItem=""
Width="100" HorizontalAlignment="Left"
Margin="50,20">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Here my ObservableCollection:
public ObservableCollection<PhaseViewModel> Phases
{
get;
}
And here the property inside Phases:
public string Name
{
get { return myName; }
set { myName = value;}
}
So I'm trying to do a program that uses ListView (instead of CheckedListBox), and I want to get the indexes of the checked elements.
I use this as a template for the ListView:
<ListView x:Name="Content" HorizontalAlignment="Left" Height="303" Margin="10,47,0,0" VerticalAlignment="Top" Width="394" ItemClick="Content_ItemClick" SelectionChanged="Content_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Selected}"></CheckBox>
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And this is the data what it got:
public class User
{
public string Name { get; set; }
public bool Selected { get; set; }
}
How could I get back the checked elements index?
I believe your ListView's item source is bound to a backing list
<ListView ItemSource"{x:Bind UserList, Mode=OneWay}" >
<ListItem.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Selected, Mode=TwoWay} Click="Click_Handler"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListItem.ItemTemplate>
</ListView>
The UserList is List<User> which contains all your User.
Since you bind the UserList to the ListView. You could retrieve the selected item by iterating through the list and read the Selected property of each individual User.
My XAML is as under. I have a main ViewModel which has a list of items and I want to display a property within this list
<ItemsControl ItemsSource="{Binding MyList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding MyName, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"></Label>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The problem is that MyName is always blank although my list has two items.
The main VM class has this property below and I add items in the constructor
public ObservableCollection<InnerViewModel> MyList { get; set; }
My inner VM has
public class InnerViewModel
{
private string _MyName;
public string MyName
{
get
{
return _MyName;
}
set
{
_MyName = value;
OnPropertyChanged("MyName");
}
}
I do have OnPropertyChanged in place but I'm not pasting it here for simplicity. I think the problem is with the XAML but I'm not sure. How do I get the property MyName to be displayed in my list of items in the view?
Since you use MyList as the ItemsSource, the data source for the child elements will be MyList. So you do not need to use the RelativeSource.
In other words, this should work :
<ItemsControl ItemsSource="{Binding MyList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding MyName}"></Label>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Try and remove the relative source part of the binding.
<DataTemplate>
<Label Content="{Binding MyName}"></Label>
</DataTemplate>
I´m struggling with data binding here for Windows Phone 7.1. I've got a DataLoader class with an OnservableCollection of ItemList (custom class) as an attribute. Therefore, each pivot item has its own list of items. In this DataLoader, I load the data from a JSON a JSON. So far, so good.
public ObservableCollection<ItemList> PivotItem { get; set; }
Here, I store five ItemList, one for each pivot header with his corresponding list of items.
But my problem is that I want to bind this data to the XAML with a ListBox within each PivotItem.
<phone:Pivot Title="iMetrópolis" Loaded="Pivot_Loaded">
<!--Elemento Pivot 1-->
<phone:PivotItem
x:Uid="PivotItem1"
Header="Todo" Margin="14,10,10,18">
<ListBox x:Name="FirstListBox" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
// I want to add textboxes binding my data
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</phone:PivotItem>
.
.
.
Thanks for the replies!!
Here is what i think you need to do , an example for databinding
<ListBox x:Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="auto" >
<TextBlock Text="{Binding PON}" />
<TextBlock Text="{Binding PIN}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and a class for it
public class ListObject
{
public string PON { get; set; }
public string PIN { get; set; }
}
and binding real data from json
dynamic json = JsonConvert.DeserializeObject(jsondata);
var questions = new List<ListObject>();
foreach (var abc in json["jsonarray"])
{
var listOfItems = new ListObject();
listOfItems.PON= abc.object1;
listOfItems.PIN= abc.object2;
questions.Add(listOfQuestions);
}
listBox1.ItemsSource = questions;
i hope it helps reply for any comments
I'm trying to build a Pivot system based on some collections, without having to build it using code-behind.
My collection is a Dictionary<CategoriesEnum, List<object>> and I'd like to bind the PivotItem's header to the CategoriesEnum object, while its content has to be bound to the related List<objet>.
Actually I've been able to just bind the PivotItem's header but I really can't do it for the List.
Here's my current code:
(XAML)
<phone:Pivot x:Name="pivot"
ItemsSource="{Binding Categories}">
<phone:Pivot.ItemTemplate>
<DataTemplate>
<phone:PivotItem Header="{Binding}">
<ListBox ItemsSource="{Binding Path=Objects}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</phone:PivotItem>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
(C#)
public List<Categories> Categories
{
get
{
return new List<Categories>(Dictionary.Keys);
}
}
public List<object> Objects
{
get
{
return Dictionary[(Categories)pivot.SelectedItem];
}
}
I know that the Objects property will never work this way, but I can't figure out how to do this type of binding and I've not found anything online to give me a clue.
Binding the ItemsSource to a dictionary enumerates the KeyValuePairs, from which you can bind to the Key and Value properties. Let's assume the Keys in your dictionary are string and the Values are enumerables (IList):
(XAML)
<phone:Pivot x:Name="pivot" ItemsSource="{Binding MyDictionary}">
<phone:Pivot.ItemTemplate>
<DataTemplate>
<phone:PivotItem Header="{Binding Key}">
<ListBox ItemsSource="{Binding Value}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MyName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</phone:PivotItem>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
(C#)
public IDictionary<string, IList<MyObject>> MyDictionary { get; set; }
public class MyObject
{
public string MyName { get; set; }
}