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>
Related
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.
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 need to display databound data in a listview such that some of the cells of the same column are underlined while others are not:
How do I create a data template that will display different control types in the same cell? (Visual Studio 2013, .NET 4.5.2)
XAML:
<ListView Grid.Column="3" ItemsSource="{Binding Results.MyItemSource, Mode=OneWay}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="Auto" Header=" " DisplayMemberBinding="{Binding Path=Caption, Mode=OneWay}"/>
<GridViewColumn Width="Auto" Header=" " DisplayMemberBinding="{Binding Path=Value, Mode=OneWay}" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
ItemSource:
public List<object> MyItemSource
{
get
{
return new List<object>
{
new CaptionValuePair {Caption = "Caption1", Value = new TextBlock {Text = "Value1"}},
new CaptionValuePair {Caption = "Caption2", Value = new TextBlock {Text = "Value2", TextDecorations = TextDecorations.Underline}}
};
}
}
Bound type:
public class CaptionValuePair
{
public string Caption { get; set; }
public object Value { get; set; }
}
Problem: The StackPanel is just displaying the typename of a textblock.
Instead of a StackPanel, using an ItemsControl and binding its ItemsSource property worked.
XAML:
<GridViewColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Path=Value, Mode=OneWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
Bound type:
public List<UIElement> Value{ get; set; }
My MVVM WPF application currently has a GridView that binds to a ViewModel property and has the columns defined in the XAML:
<ListView Grid.Row="0" ItemsSource="{Binding GroupedOrders}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Item2, Mode=OneWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Date" DisplayMemberBinding="{Binding Item1.Date, StringFormat={}{0:dd/MM/yyyy}}" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Item1.Name}" />
<!-- lots more -->
</GridView>
</ListView.View>
</ListView>
GroupedOrders is an ObservableCollection of a Tuple of two different item types: an "Order" object and a Boolean that controls whether or not, for this particular view, it is "selected".
However, that "selected" property hasn't been modelled well. It's come to light that each Order needs multiple "selected" properties, depending on the number of dates in the order, which can be between one and five.
To model this in the UI, I need to replace that single Checkbox GridViewColumn with a dynamic construct that creates a similar Checkbox GridviewColumn for each date in the order. So GroupedOrders becomes a Tuple <Order, List<bool>> instead, and there will need to be one column for each bool in the List.
At any given instance, the size of that list will be the same for all the Orders in the grid. But if the user loads new data into the grid, the size of the list will change.
However, I cannot see how to do this in MVVM. Existing solutions seem to be for code-behind where the GridView can be grabbed as an object and manipulated on the fly. The only MVVM solution I've seen to this is to use a Converter to building the entire GridView on the fly, which would seem to be massive overkill for this situation, where there are a lot of columns in the GridView but only a small number need to be dynamic.
Is there another way?
Not sure this is what you expect, but this is what I can think of.
View
<ListView ItemsSource="{Binding Orders}">
<ListView.View>
<GridView>
<GridViewColumn Header="State">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding States}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.Resources>
<Style TargetType="GridViewColumnHeader">
<Setter Property="Visibility" Value="Collapsed" />
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Value}" Content="{Binding Text}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="OrderNo" DisplayMemberBinding="{Binding OrderNo}" />
</GridView>
</ListView.View>
</ListView>
Code Behind
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Orders.Add(new Order { OrderNo = "Order001" });
Orders.Add(new Order { OrderNo = "Order002" });
Orders.Add(new Order { OrderNo = "Order003" });
}
private readonly ObservableCollection<Order> _orders = new ObservableCollection<Order>();
public ObservableCollection<Order> Orders
{
get { return _orders; }
}
}
public class Order
{
public Order()
{
States.Add(new State { Text = "Is Paid", Value = false });
States.Add(new State { Text = "Is Delivered", Value = false });
}
public string OrderNo { get; set; }
private readonly ObservableCollection<State> _states = new ObservableCollection<State>();
public ObservableCollection<State> States
{
get { return _states; }
}
}
public class State
{
public string Text { get; set; }
public bool Value { get; set; }
}
Result
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.