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>
Related
I have ListView binding with my collection:
private List<MyData> Col;
ItemsSource="{Binding Col}"
I define this inside my ListView:
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Selected">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="cbSelect" IsChecked="{Binding SupportedVendors}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding ID}" Header="ID" Width="600"/>
</GridView.Columns>
</GridView>
</ListView.View>
Now i want channge my object bool value inside my Collcection once the user change the CheckBox.
I can i do that ? (i need it in code behaind)
Tag the object of your collection with the CheckBox, the on check /uncheck you can get the tag from the checkbox checked. Change the bool value in the object.
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>
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>
i am trying to use styles to prevent from repeting code, by putting them in a Resource Disctionary.
My question is, when we have a GridViewColumn in a ListView, which one of the columns have a DataTemplate, and in that DataTemplate we have the CellTemplate with only a CheckBox, can we bind the CheckBox state when the DataTemplete is in a ResourceDictionary?
What i have is this in my XAML:
<ListView Name="listView">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn DisplayMemberPath="{Binding [1]}"/>
<GridViewColumn DisplayMemberPath="{Binding [2]}"/>
<GridViewColumn DisplayMemberPath="{Binding [4]}"/>
<GridViewColumn DisplayMemberPath="{Binding [5]}"/>
<GridViewColumn DisplayMemberPath="{Binding [6]}"/>
<GridViewColumn DisplayMemberPath="{Binding [7]}"/>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsThreeState="False" IsChecked="{Binding [8]}" Unchecked="CheckBox_Changed" Checked="CheckBox_Changed"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
And i am trying to do something like this in the Resource Dictionary:
<DataTemplate x:Key="ListViewCheckboxCell">
<StackPanel>
<CheckBox IsThreeState="False" IsChecked="Make reference"/>
</StackPanel>
</DataTemplate>
And the values in that column is always a bool.
Thanks in advance!
What you did seems correct. You'll now have to write
<GridViewColumn CellTemplate="{StaticResource ListViewCheckboxCell}" />
The template will be taken into account, you can leave exactly the same template than the original in your resource dictionary: The binding is dynamically resolved, so when the XAML will be read, bindings will automatically be set to the related object, following what you indicated
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)