I need to display all processes, namely: Process name, ID, and priority in ListView
I tried:
public Process[] processes;
public string[] processesss;
public MainWindow() {
InitializeComponent();
processes = Process.GetProcesses();
ProcessInfo.ItemsSource = processes;
}
XAML:
<ListView Name="ProcessInfo">
<ListView.View>
<GridView>
<GridViewColumn Header="Process Name" Width="100" DisplayMemberBinding="{ProcessName}"/>
<GridViewColumn Header="Process ID" Width="100" DisplayMemberBinding="{ID}"/>
<GridViewColumn Header="Status" Width="70"/>
</GridView>
</ListView.View>
</ListView>
try this
InitializeComponent();
processes = Process.GetProcesses();
List<processlist> processlist = new List<processlist>();
foreach (Process item in processes)
{
processlist.Add(new processlist() { id = item.Id, name = item.ProcessName });
}
ProcessInfo.ItemsSource = processlist;
and add this class
public class processlist
{
public int id { get; set; }
public string name { get; set; }
}
WPF UI
<Grid >
<ListView Margin="10" Name="ProcessInfo">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding name}" />
<GridViewColumn Header="id" Width="50" DisplayMemberBinding="{Binding id}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
You're on the right track. Just make sure your UI element has got correct binding information to pick properties from the ItemsSource. Could be a bit easier for you if you replace ListView with a DataGrid and set its AutoGenerateColumns to True. This will automatically start showing your processes in the grid.
On the other hand, you can use ListView or even ListBox for your purpose, but then you'll have to set up ItemTemplate for your control, which is a broad topic in itself.
Related
As someone who has recently switched from WinForms to WPF, I'm still struggling and going nuts trying to figure out a way to loop through and delete checked ListView items.
This method gives me error: "ListView does not contain a definition for CheckedItems..."
if (lvFilesList != null)
{
foreach (ListViewItem lvItem in lvFilesList.CheckedItems)
{
lvItem.Checked = False;
}
}
My XAML Code:
<ListView Height="400" Width="400"
Name="lvFilesList"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chk" IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"/>
<GridViewColumn Header="File" DisplayMemberBinding="{Binding File}"/>
<GridViewColumn Header="Author" DisplayMemberBinding="{Binding Author}"/>
</GridView.Columns>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" Text="Group"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
It looks odd that the ItemsSource of your ListView is directly bound to the current DataContext by
ItemsSource="{Binding}"
The DataContext would usually hold a view model object with a collection-type property like
public class Item
{
public bool IsChecked { get; set; }
// other properties like ID, File, Author
}
public class ViewModel
{
public ObservableCollection<Item> Items { get; }
= new ObservableCollection<Item>();
}
and the Binding would be
ItemsSource="{Binding Items}"
Then the view model could have a method that deletes all checked items, like
public void DeleteCheckedItems()
{
var checkedItems = Items.Where(item => item.IsChecked).ToList();
checkedItems.ForEach(item => Items.Remove(item));
}
Note that you usually assign an instance of the view model class to the DataContext of your main view, e.g. in the MainWindow constructor:
private readonly ViewModel viewModel = new ViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = viewModel;
viewModel.Items.Add(new Item { ID = 1, ... });
viewModel.Items.Add(new Item { ID = 2, ... });
}
I have been experiencing problems with WPF ListView (using elements binding), as I tried to initialize it by injecting ItemsSource the list of Disks, and got no visual feedback from the element. I wrote my code using the example supplied here.
Here are the relevant code parts:
Setting the ListView
private void viewDisk_Click(object sender, RoutedEventArgs e)
{
List<DiskDetails> data = new List<DiskDetails>();
foreach(Disk disk in disks)
data.Add(new DiskDetails(disk.GetVolumeHeader().DiskName, disk.GetVolumeHeader().DiskOwner,disk.GetVolumeHeader().ProdDate));
disksList.ItemsSource = data;
}
DiskDetails Class
public class DiskDetails
{
public string diskName { get; set; }
public string diskOwner { get; set; }
public string cDate { get; set; }
public DiskDetails(string dN, string dO,string cD)
{
diskName = dN;
diskOwner = dO;
cDate = cD;
}
}
WPF ListView
<Grid Grid.Column="0">
<ListView x:Name="disksList" VerticalAlignment="Top" Height="250" SelectionChanged="disksList_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Disk Name" Width="108" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Disk Owner" Width="108" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Creation Date" Width="108" DisplayMemberBinding="{Binding Mail}" />
</GridView>
</ListView.View>
</ListView>
<Button x:Name="viewDisk" Content="View Disk" Width="90" Height="40" VerticalAlignment="Bottom" Margin="0,0,0,15" Click="viewDisk_Click"/>
</Grid>
Thanks.
Looks like your DisplayMemberBinding wasn't changed from the example code. Try changing the bindings to match the properties of DiskDetails. E.g. DisplayMemberBinding="{Binding Name}" should change to DisplayMemberBinding="{Binding diskName}"
I want to change each columns font size. I am using listview with Gridview. I want the result something like this:
Here is my XML Code:
<ListView x:Name="listView1" Margin="0,0,-5,-5" FontSize="20">
<ListView.View>
<GridView>
<GridViewColumn Header="Column1" DisplayMemberBinding="{Binding c1}" />
<GridViewColumn Header="Column2" DisplayMemberBinding="{Binding c2}" Width="780"/>
<GridViewColumn Header="Column3" DisplayMemberBinding="{Binding c3}" Width="460"/>
</GridView>
</ListView.View>
</ListView>
C#:
this.listView1.Items.Add(new MyItem { c1= "This is Column 1 Row 1 Text", c2="This is Column 2 Row 1 Text", c3= "This is Column 3 Row 1 Text" });
class MyItem
{
public string c1{ get; set; }
public string c2{ get; set; }
public string c3{ get; set; }
}
You can always use the CellTemplate instead of DisplayMemberBinding to have full customization options.
For the first column something like:
<GridViewColumn Header="Column1">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding c1}" FontSize="15px"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
I am updating a listbox via databind and one of the columns I am trying to insert is a checkbox. This update is being processed by a threadpool and I am able to insert the data fine, except for the checkbox. When I create the checkbox its displaying the xaml instead of the checkbox element.
i.e.
System.Windows.Controls.Checkbox Content: IsChecked:False
Definition of NotesReminderViewDetails
private struct NotesRemindersViewDetails
{
public string NoteReminderID { get; set; }
public string NoteReminderEnterDate { get; set; }
public string NoteReminderDueDate { get; set; }
public string NoteReminderConents { get; set; }
public CheckBox NoteReminderCompleted { get; set; }
}
Here is the code I am using to update the listview. NoteReminderType is a struct with all the note/reminder information.
NoteReminderType noteType = noteReminder.NoteReminderDetails;
NotesRemindersViewDetails noteReminderDetails = new NotesRemindersViewDetails();
noteReminderDetails.NoteReminderID = noteType.UserFriendlyNoteReminderID.ToString();
noteReminderDetails.NoteReminderEnterDate = noteType.InsertionDate.ToShortDateString();
noteReminderDetails.NoteReminderDueDate = noteType.DueDate.ToShortDateString();
noteReminderDetails.NoteReminderConents = noteType.Description;
listViewNotesReminders.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
{
noteReminderDetails.NoteReminderCompleted = new CheckBox();
listViewNotesReminders.Items.Add(noteReminderDetails);
}));
What do I need to change to get the checkbox to be displayed instead of the xaml form the threadpool thread?
EDIT
Here is the xaml code for the listview
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="20" DisplayMemberBinding="{Binding Path=NoteReminderID}" />
<GridViewColumn Header="Entered Date" Width="Auto" DisplayMemberBinding="{Binding Path=NoteReminderEnterDate}" />
<GridViewColumn Header="Due Date" Width="75" DisplayMemberBinding="{Binding Path=NoteReminderDueDate}" />
<GridViewColumn Header="Note Contents" Width="300" DisplayMemberBinding="{Binding Path=NoteReminderConents}" />
<GridViewColumn Header="Completed" Width="Auto" DisplayMemberBinding="{Binding Path=NoteReminderCompleted}" />
</GridView>
</ListView.View>
Rather than putting a UI element (CheckBox) in the data for the ListView, you should define a template so you can render the column as a checkbox, and just use a Boolean for the data. Using a CheckBox in your data is mixing your UI and data layers.
Update:
Here's an example (not tested) of how to make a custom column template:
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="20"
DisplayMemberBinding="{Binding Path=NoteReminderID}" />
<GridViewColumn Header="Entered Date" Width="Auto"
DisplayMemberBinding="{Binding Path=NoteReminderEnterDate}" />
<GridViewColumn Header="Due Date" Width="75"
DisplayMemberBinding="{Binding Path=NoteReminderDueDate}" />
<GridViewColumn Header="Note Contents" Width="300"
DisplayMemberBinding="{Binding Path=NoteReminderConents}" />
<GridViewColumn Header="Completed" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=NoteReminderCompleted}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
You need to use bool field for NoteReminderCompleted instead of CheckBox.
In WPF app I have a WCF service which dynamically fills a generic List object from a backend database.
How in this case (List created in runtime), I could bind List items to a ListView object items?
It is the Data contract for my Web service:
....
[DataContract]
public class MeetList
{
[DataMember]
public string MeetDate;
[DataMember]
public string MeetTime;
[DataMember]
public string MeetDescr;
.....
static internal List<MeetList> LoadMeetings(string dynamicsNavXml)
{
...// Loads XML stream into the WCF type
}
Here in this event handler I read the WCF service and Loop through a List object:
private void AllMeetings()
{
Customer_ServiceClient service = new Customer_ServiceClient();
foreach (MeetList meet in service.ReadMeetList())
{
?????? = meet.MeetDate; // it's here that I bumped into a problem
?????? = meet.MeetTime; //
?????? = meet.MeetDescr;//
}
}
My Listview XAML:
<Grid>
<ListView Height="100" Width="434" Margin="0,22,0,0" Name="lvItems" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="Date" Width="100" HeaderTemplate="{StaticResource DateHeader}" CellTemplate="{DynamicResource DateCell}"/>
<GridViewColumn Header="Time" Width="100" HeaderTemplate="{StaticResource TimeHeader}" CellTemplate="{DynamicResource TimeCell}"/>
<GridViewColumn Header="Description" Width="200" HeaderTemplate="{StaticResource DescriptionHeader}" CellTemplate="{DynamicResource DescriptionCell}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
And data templates for this ListView:
<Window.Resources>
<DataTemplate x:Key="DateHeader">
<StackPanel Orientation="Horizontal">
<TextBlock Margin="10,0,0,0" Text="Date" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="DateCell" DataType="Profile">
<StackPanel Orientation="Horizontal">
<TextBlock>
<TextBlock.Text>
<Binding Path="MeetDate" />
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
......
How in this case (List created in runtime), I could bind my generic List items to a ListView object items?
I tried to use lvItems.ItemsSource = profiles; , but it doesn't work in my event handler
List doesn't have behaviour to notify that items count is changed. You should use a list with support INotifyCollectionChanged.. for example: ObservableCollection<T>. ObservableCollection<T> will inform your lvItems that items count is changed and it will be properly display.
Using intermediate ObservableCollection:
ObservableCollection<Meets> _MeetCollection =
new ObservableCollection<Meets>();
public ObservableCollection<Meets> MeetCollection
{ get { return _MeetCollection; } }
public class Meets
{
public string Date { get; set; }
public string Time { get; set; }
public string Description { get; set; }
}
In the event handler loop we put:
private void AllMeetings()
{
Customer_ServiceClient service = new Customer_ServiceClient();
_MeetCollection.Clear();
foreach (MeetList meet in service.ReadMeetList())
{
_MeetCollection.Add(new Meets
{
Date = meet.MeetDate,
Time = meet.MeetTime,
Description = meet.MeetDescr
});
}
}
And XAML binding is changed to:
<Grid>
<ListView Height="100" Width="434" Margin="0,22,0,0" Name="lvItems" ItemsSource="{Binding MeetCollection}">
<ListView.View>
<GridView>
<GridViewColumn Header="Date" Width="100" DisplayMemberBinding="{Binding Date}"/>
<GridViewColumn Header="Time" Width="100" DisplayMemberBinding="{Binding Time}"/>
<GridViewColumn Header="Description" Width="200" DisplayMemberBinding="{Binding Description}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>