WPF control ListView - wpf

In C# ListView -> ListItem -> subItem.
How to add a new entry to a list using WPF?

To add data such as ListView WF, you must use binding.
For example:
<ListView ItemsSource="{Binding}" x:Name="lv">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
Show data of two column(ID, Name) with this example

Your question is hardly intelligible but let's just shoot into the blue:
Adding items in XAML:
<ListView>
<ListViewItem>
<!-- Item Content -->
</ListViewItem>
</ListView>
In code behind:
listView.Items.Add(new ListViewItem() { Content = content });
(ListViewItems do not have sub-items by the way)

Related

WPF&MVVM: Add controls to auto-generated List View

I want to create the List View like this:
(Although the List View already has multi-select function and don't need the checkboxes, I want to add them for low-experience user who don't know what if to hold Shift key it's possible to select multiple items.)
Currently, my List View includes only ID and Full Name columns; the data displays by binding created according MVVC concept.
<ListView Name="DataTable" ItemsSource="{Binding Path=people}">
<ListView.View>
<GridView>
<GridViewColumn Width="50px">
<!-- This is column for checkboxes but it don't should be in header -->
</GridViewColumn>
<GridViewColumn Header="ID"
DisplayMemberBinding="{Binding PersonID, UpdateSourceTrigger=PropertyChanged}">
</GridViewColumn>
<GridViewColumn Header="FullName"
DisplayMemberBinding="{Binding FullName, UpdateSourceTrigger=PropertyChanged}" />
<GridViewColumn Width="50px">
<!-- This is column for "Edit" buttons but it don't should be in header -->
</GridViewColumn>
<GridViewColumn Width="50px">
<!-- This is column for "Delete" buttons but it don't should be in header -->
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
How I can add the checkboxes and buttons in every not-header row?
You should declare a CellTemplate with a CheckBox:
<GridViewColumn Width="50px">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding MyProperty}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

WPF: When Adding items to a listview they become empty

Im a bit new to WPF and I cant seem to figure out why the items I add to a listview are displaying empty.
Here is my XAML
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="30" DisplayMemberBinding="{Binding Source={StaticResource BillingIncrements}, Path=ID}" />
<GridViewColumn Header="Bill Group" DisplayMemberBinding="{Binding Source={StaticResource BillingIncrements}, Path=Description}" />
</GridView>
</ListView.View>
Here is the code
lstBillGroups.Items.Add(New StlReportPlugins.BillingIncrements With {.ID = 100, .Description = "Test"})
Here is the output
(couldnt post a pic because i dont have enough points)
Link to pic
Any Ideas?
This is how your XAML should look like
<ListView ItemsSource={Binding Source={StaticResource BillingIncrements}}>
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="30" DisplayMemberBinding="{Binding ID}" />
<GridViewColumn Header="Bill Group" DisplayMemberBinding="{Binding Description}"/>
</GridView>
</ListView.View>
</ListView>
If you have a look at output window in VS while debugging your app you'll see a bunch of WPF binding errors, which helps a lot to spot mistakes.

Treeview with Grid inside in WPF

I want to display a simple schema of a database in a treeview with 1 level of depth.
So I have a collection of tables, whose name I'd like to display as a header of the elements, and for each table a collection of columns with name, type, etc..
However in each table I want to display those columns definition in a grid, with a header specifying the properties of the columns definition, and in each row, the definition itself.
One way to make this work is to see that as a treeview where each table has a sequence of one child (the entire collection of column definition), then displayed as a grid :
<TreeView ItemsSource="{Binding Tables}" x:Name="treeView" MinWidth="400" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=ListofONEelement}">
<TextBlock Text="{Binding Path=TableName}" />
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding GroupOfColumnDefinition}">
<ListView.View>
<GridView>
<GridViewColumn Header="ColumnName" DisplayMemberBinding="{Binding Path=Item1}" />
<GridViewColumn Header="Model" DisplayMemberBinding="{Binding Path=Item2.Model}" />
<GridViewColumn Header="ColumnStatus" DisplayMemberBinding="{Binding Path=Item2.ColumnStatus}" />
<GridViewColumn Header="ColumnType" DisplayMemberBinding="{Binding Path=Item2.ColumnType}" />
</GridView>
</ListView.View>
</ListView>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
But I had to create this intermediate structure ListofONEelement to make this works, as hierarchy in the treeview exists only if there are items to enumerate.
Are there other, nicer ways to do this, directly in XAML, that would not require to have a custom type created in my ViewModel ?
Actually put the one item in the TreeView. Don't use HierarchicalDataTemplate.
<TreeView ItemsSource="{Binding Tables}">
<TreeView.ItemTemplate>
<DataTemplate>
<TreeViewItem Header="{Binding TableName}">
<ListView ItemsSource="{Binding GroupOfColumnDefinition}">
<ListView.View>
<GridView>
<GridViewColumn Header="ColumnName" DisplayMemberBinding="{Binding Path=Item1}" />
<GridViewColumn Header="Model" DisplayMemberBinding="{Binding Path=Item2.Model}" />
<GridViewColumn Header="ColumnStatus" DisplayMemberBinding="{Binding Path=Item2.ColumnStatus}" />
<GridViewColumn Header="ColumnType" DisplayMemberBinding="{Binding Path=Item2.ColumnType}" />
</GridView>
</ListView.View>
</ListView>
</TreeViewItem>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>

WPF Databinding ListView to a Property of an object which can be nothing

I'm trying to create a Window with a ListView and an Area where details to the selected Object are displayed. The Listview displays items stored in an ObservableCollection(Of T) Collection. The items itself contain also an ObservableCollecton(Of T) Collection which should then be displayed in the details area in another ListView, accordingly to the selected item of the first ListView.
The Problem:
The InitializeComponent() throws an Exception (XAMLParseException).
Exception:
Set property 'System.Windows.Controls.GridViewColumn.DisplayMemberBinding' threw an exception.
InnerException:
Object of type 'System.String' cannot be converted to type 'System.Windows.Data.BindingBase'.
The Line- and ColumNumer of the Exception are Pointing at the <GridView> of my ListView (.View)
This is the First ListView
<ListView ItemsSource="{Binding Path=MyObjectCollection, Mode=OneWay}" SelectedItem="{Binding Path=Selected, Mode=OneWayToSource}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=Name, Mode=OneWay}">
<GridViewColumnHeader Content="Name" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
And this is the second ListView
<ListView ItemsSource="{Binding Path=SelectedItem.MySubCollection, Mode=OneWay}">
<ListView.View>
<GridView> <!-- Thats the Line where the Exception is pointing at -->
<GridViewColumn Width="150" DisplayMemberBinding="Key">
<GridViewColumnHeader Content="Key" />
</GridViewColumn>
<GridViewColumn Width="150" DisplayMemberBinding="Value">
<GridViewColumnHeader Content="Value" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
So. How can I bind to the Collection Property of an Object could be nothing?
From the exception message the problem is with with your DisplayMemberBinding in the second listview. Because you have to provide a Binding expression instead of a string see MSDN. Like in your first listview:
<ListView ItemsSource="{Binding Path=SelectedItem.MySubCollection, Mode=OneWay}">
<ListView.View>
<GridView> <!-- Thats the Line where the Exception is pointing at -->
<GridViewColumn Width="150" DisplayMemberBinding="{Binding Path=Key}">
<GridViewColumnHeader Content="Key" />
</GridViewColumn>
<GridViewColumn Width="150" DisplayMemberBinding="{Binding Path=Value}">
<GridViewColumnHeader Content="Value" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

how to template a listview item / wpf

I am trying to achieve this using Expression Blend 3. I need that every listview item to contain an image, followed by a text. ItemTemplate doesn't seem to work.
Thanks
Listview is differnt then the List control. For ListView you have to add a "View" to the object, then define your columns.
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Column 1" />
<GridViewColumn Width="140" Header="Column 2" />
<GridViewColumn Width="140" Header="Column 3" />
</GridView>
</ListView.View>
</ListView>
you should check this tutorial.

Resources