I am making a simple windows forms application. In my listview i want to display three things; date, time and description. I am now doing it like this:
task = date + time + description;
(task, date, time, description are all strings).
which give the result:
But what i want is to make these strings to display below the different categories, something like this:
How would go about formatting the 3 strings (date,time,desciption) into one (task) so that they display as in picture two?
答案 0 :(得分:1)
Concatenating items with +
will just perform normal string concatenation, which is why you get everything in a single lump.
To do what you ask you have to add a new ListViewItem
where you specify a string array in the constructor. Each array element represents a column in the ListView
: {column 1, column 2, column 3, etc...}
For example:
ListView1.Items.Add(new ListViewItem(new string[] {date, time, description}));
MSDN Documentation for the ListViewItem(string[])
constructor: https://msdn.microsoft.com/en-us/library/faw83h4f(v=vs.110).aspx
答案 1 :(得分:1)
You need to set View:Details
from ListView properties to give a grid-style look. When you create a ListItem, it is treated as the data of first column and subsequent SubItems are treated as extra columns for that particular ListItem. Try this example.
ListViewItem lvi;
for (int i = 0; i < 10; i++)
{
lvi = new ListViewItem();
lvi.Text = "first column";
lvi.SubItems.Add("second column");
lvi.SubItems.Add("third column");
listView1.Items.Add(lvi);
}
答案 2 :(得分:1)
Here's a bit of an expanded example for you.
While you may find a way to concatenate the strings and then space them properly by displaying them in something other than a listview, a listview is going to be your best option.
Your code will look something akin to this:
public partial class MainWindow : Window
{
//Your data
private string date = "2016-04-04";
private string time = "20:20";
private string description = "poop";
//Declare a list
public List<object> myList { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
//Instantiate your list
myList = new List<object>();
//Make a new object
var listObject = new
{
newDate = date,
newTime = time,
newDescription = description
};
//Add that object to your list
myList.Add(listObject);
}
}
And your XAML to display your results:
<Window x:Class="WpfApplicationTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView x:Name="listView" ItemsSource="{Binding myList}">
<ListView.View>
<GridView>
<GridViewColumn Header="Date" DisplayMemberBinding="{Binding newDate}"/>
<GridViewColumn Header="Time" DisplayMemberBinding="{Binding newTime}"/>
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding newDescription}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>