How to bind a IEnumerable<string> to a ListBox? - c#

This may be a very simple question, but at the moment I don't actually know what to google.
If I had an object like this:
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
In my VM I have a list of employees (public List<Employee> Employees). It's easy to bind this in my Xaml to a ListBox:
<ListBox ItemsSource={Binding Employees}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But what if my list of employees would contain strings and not Employee objects? How could I bind these string values to the TextBlock in the data template?

Just write {Binding} where you specify the binding.

Related

TextBlock not showing up in ListView

This is my ListView:
<ListView ItemsSource="{Binding ModuleList}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!--<Image Source="{Binding ModuleImage}" Stretch="UniformToFill"/>-->
<TextBlock Text="{Binding ModuleName}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This is the code in WPF (MVVM):
public ItemListViewVM() : base()
{
ModuleList = new List<Module>();
modulLoader.LoadAllModules();
tempModulList = modulLoader.GetAllModules();
foreach (var module in tempModulList)
{
ModuleImage = module.ModuleImage;
ModuleName = module.Name;
ModuleList.Add(module);
}
}
Long story short: The List tempModulList contains Objects of type Module, which has an ImageSource Image and a string Name. Then the ModuleList gets one item after another. When I uncomment the Image in xaml, you can see it. But the TextBlock won't show up no matter what. I checked the string module.Namefor every item, it is not empty.
EDIT: Add Module Class
The Module Class just contains Name and Image:
public class Module
{
public ImageSource ModuleImage { get; set; }
public string Name { get; set; }
}
The Object gets created by deserializing a Json
The Bindings in the ItemTemplate use the properties of the data item class as their source properties. Hence you should write
Text="{Binding Name}"
instead of
Text="{Binding ModuleName}"
Unless you set the View property of a ListView (to e.g. a GridView) you could also better use a ListBox, which is the base class of ListView, and simpler:
<ListBox ItemsSource="{Binding ModuleList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ModuleImage}"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Initializing the ModuleList property in the view model constructor would be as simple as this:
public ItemListViewVM()
{
modulLoader.LoadAllModules();
ModuleList = modulLoader.GetAllModules();
}

binding to a static list and properties xamarin forms

I am making a portable Xamarin project. I have the following List in the class Stash:
public class Stash
{
public static List<Group> Groups { get; set; }
}
and a class Group:
public class Group
{
[JsonProperty(PropertyName ="Name")]
public string Name { get; set; }
[Newtonsoft.Json.JsonProperty("Id")]
public string GroupId { get; set; }
}
In another page in XAML I want to bind to a static list. I have a ListView that I want to bind to the Group List:
<ListView ItemsSource="{Binding Source={x:Static local:Stash.Groups}}"
IsGroupingEnabled="true">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" TextColor="Black"
Detail="{Binding GroupId}" DetailColor="Aqua">
</TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I defined the Namespace in the local in XAML:
xmlns:local="clr-namespace:Namespace"
My List is filled from the Sql Azure Database so it is not empty guys , Can anybody tell me what is wrong with my code? I cant seem to display the list on my screen , All what is displayed is this sentence Namespace.Group .
The following binding is incorrect. You don't need to
<ListView ItemsSource="{Binding Source={x:Static local:Stash.Groups}}"
You should change this to
<ListView ItemsSource="{Binding {x:Static local:Stash.Groups}"
Databinding Xamarin ListView
its working , i just removed IsGroupingEnabled="true" and it worked
<ListView ItemsSource="{Binding Source={x:Static local:Stash.Groups}}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" TextColor="Black"
Detail="{Binding GroupId}" DetailColor="Aqua">
</TextCell>
</DataTemplate>
</ListView.ItemTemplate>

Databinding of a tabcontol -wpf

I want to design a tabcontrol. Each tabitem has a grid. Lets say grid is databound to list of object. Now how do i bind the tabcontrol to.
Below shown is the class
Class GridData
{
String Name;
string Id;
string location
}
Class TabItems
{
string tabName;
List<GridData> ls;
}
Whats the best way to implementing the databinding
Say you have these 2 classes:
public class GridData
{
public string Name { get; set; }
public string Id { get; set; }
public string Location { get; set; }
}
public class TabItems
{
public string tabName { get; set; }
public List<GridData> ls { get; set; }
}
and you set TabControl.ItemsSource to List<TabItems> then your XAML for that TabControl can look something like this:
<TabControl>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding tabName}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding ls}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Id, StringFormat='{}Id: {0}'}"/>
<TextBlock Text="{Binding Path=Name, StringFormat='{}Name: {0}'}"/>
<TextBlock Text="{Binding Path=Location, StringFormat='{}Location: {0}'}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
You specify 2 DataTemplates. TabControl.ItemTemplate for header and TabControl.ContentTemplate for content which in this example is ItemsControl to display ls

Binding Selected Item in a Listbox of Items to a property?

I have a listbox which is bound to a list of Currencies items. Currency is a class
public class Currency
{
public string code { get; set; }
public string countryName { get; set; }
public string imgUrl { get; set; }
public string infoLink { get; set;} }
}
A list box is bound to a list of Currencies Objects and each item in this listbox is a stackpanel of an image and textblock
I want to bind the SelectedItem property to a property in the Code-behind to keep up
<ListBox Name="sCurrencyLB" Margin="10,0,0,0" Width="Auto" Height="180"
IsEnabled="{Binding IsChecked, ElementName=LiveTilesToggleBtn}"
SelectedItem="{Binding STileCurrency, Mode=TwoWay,
Source={StaticResource livetilemanager}}"
ItemsSource="{Binding SCurrencyList}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Name="scountryNametb" Width="50" Text="{Binding code}"
VerticalAlignment="Center" HorizontalAlignment="Right"/>
<Image Source="{Binding imgUrl}" Height="50" Width="50"
HorizontalAlignment="Left" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code of the property that should beind the selected item in the listbox
private Currency sTileCurrency;
public Currency STileCurrency
{
get
{
return appSettings.GetValueorDefault<Currency>("STileCurrency", null);
}
set
{
if (appSettings.AddOrUpdateValue("STileCurrency", value))
{
settings.Save();
}
}
}
Note : I created an instance of Class containing the property inside XAML
Follow MVVM Pattern(pretty) that's what you need
try this:
befor InitializeComponent(); in your contractor of window add this:
this.DataContext = this;
so bind the SelectedItem like this
SelectedItem="{Binding STileCurrency,Mode=TwoWay}
What you have should work if livetilemanager here:
Source={StaticResource livetilemanager}}
Has a property on it:
SCurrencyList
Does it? If SCurrencyList is a property in your code-behind - since the code-behind is the default DataContext - you do not need to specify a Source for you SelectedItem binding.
By the way to see binding errors keep an eye on the Debug Window in VS while debugging.
Incidentally in C# it typical to name properties with capital first letter, e.g.:
public string Code {get;set;}

Windows Phone Bind Listbox With a Listbbx

I have a class with a list of images in it.
public class RSSClass
{
public string Title { get; set; }
public string Summary { get; set; }
public string PubDate { get; set; }
public List<ImageData> ImagePath { get; set; }
public class ImageData
{
public Uri ImageLocation
{
get;
set;
}
}
}
Hers the XAML
<ListBox Name="lstRSS">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel.Resources>
</StackPanel.Resources>
<TextBlock Text="{Binding Path=Title}"> </TextBlock>
<TextBlock Text="{Binding Path=PubDate}"></TextBlock>
<TextBlock Text="{Binding Path=Summary}"></TextBlock>
<ListBox x:Name="ImageListBox" ItemsSource="{Binding ImagePath}">
<DataTemplate>
<Image x:Name="image1" Source="{Binding}" MaxHeight="80" MaxWidth="120" Margin="0"></Image>
</DataTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The inner list box, ImageListBox, binds and shows the string value of the Uri. Im just not sure what to bind the image in the inner list box, image1, to?
Any help much appreciated.
Regards
Ross
You are missing <ListBox.ItemTemplate> around your <DataTemplate>. This confuses ListBox, so it thinks it is actualy an item, not a template.
Once you implement #Euphoric's resolution, bind the image1's source to ImageLocation. Example:
<Image x:Name="image1" Source="{Binding ImageLocation}" MaxHeight="80" MaxWidth="120" Margin="0"></Image>

Categories