Answered by:
ListView Binding

Question
-
I've got a listview grid style layout and on one column I want my integer value to reflect a word - for example - 1 = Complete, 2 = Pending
my data is bound to a datatable which is filled from a sql search
how can I do this?
Tuesday, September 4, 2012 6:39 AM
Answers
-
You can copy paste code below for the sample(Create a project names: ListViewTest):
XAML:
<Window x:Class="ListViewTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListViewTest"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<local:IntToStringConverter x:Key="Conv" />
</Window.Resources>
<Grid>
<ListView x:Name="ListStudent" ItemsSource="{Binding StatusList}">
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel LastChildFill="False">
<TextBlock Text="{Binding Converter={StaticResource Conv}}" VerticalAlignment="Center"
DockPanel.Dock="Left" />
<TextBlock Margin="10" Text="Sample" HorizontalAlignment="Right" />
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ListViewTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
List<int> mStatusList;
public Window1()
{
InitializeComponent();
DataContext = this;
mStatusList = new List<int>() { 1, 2, 1, 1, 2, 2, 1 };
}
public List<int> StatusList
{
get
{
return mStatusList;
}
}
}
public class IntToStringConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int val = (int)value;
if (val == 1)
{
return "Complete";
}
return "Pending";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
Regards
Agrawal
-------------------------------------------------------------------------
Please remember to mark the replies as answers and helpful if they help.
- Edited by agrawal.ashish Tuesday, September 4, 2012 7:01 AM
- Proposed as answer by noorbakhsh Tuesday, September 4, 2012 2:35 PM
- Marked as answer by Kee Poppy Wednesday, September 12, 2012 11:06 AM
Tuesday, September 4, 2012 7:00 AM
All replies
-
you can use a IValueConverter Interface to reflect the corresponding meaning of your integer values.
If you are looking for a sample please let me know.
Regards
Ashish
-------------------------------------------------------------------------
Please remember to mark the replies as answers and helpful if they help.
- Edited by agrawal.ashish Tuesday, September 4, 2012 6:50 AM
Tuesday, September 4, 2012 6:47 AM -
You can copy paste code below for the sample(Create a project names: ListViewTest):
XAML:
<Window x:Class="ListViewTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListViewTest"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<local:IntToStringConverter x:Key="Conv" />
</Window.Resources>
<Grid>
<ListView x:Name="ListStudent" ItemsSource="{Binding StatusList}">
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel LastChildFill="False">
<TextBlock Text="{Binding Converter={StaticResource Conv}}" VerticalAlignment="Center"
DockPanel.Dock="Left" />
<TextBlock Margin="10" Text="Sample" HorizontalAlignment="Right" />
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ListViewTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
List<int> mStatusList;
public Window1()
{
InitializeComponent();
DataContext = this;
mStatusList = new List<int>() { 1, 2, 1, 1, 2, 2, 1 };
}
public List<int> StatusList
{
get
{
return mStatusList;
}
}
}
public class IntToStringConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int val = (int)value;
if (val == 1)
{
return "Complete";
}
return "Pending";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
Regards
Agrawal
-------------------------------------------------------------------------
Please remember to mark the replies as answers and helpful if they help.
- Edited by agrawal.ashish Tuesday, September 4, 2012 7:01 AM
- Proposed as answer by noorbakhsh Tuesday, September 4, 2012 2:35 PM
- Marked as answer by Kee Poppy Wednesday, September 12, 2012 11:06 AM
Tuesday, September 4, 2012 7:00 AM