Treeview with Grid inside in WPF - 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>

Related

How to bind double click on row to command in GridView that inside ListView in MVVM WPF

I'm prrety new to wpf, and i try to make a GridView that inside ListView to response to double click on row in the view
I try to make this-
<ListView
ItemsSource="{Binding Products}">
<ListView.InputBindings>
<MouseBinding
MouseAction="LeftDoubleClick"
Command="{Binding Test}" />
</ListView.InputBindings>
<ListView.View>
<GridView>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn
Header="ID"
DisplayMemberBinding="{Binding ID}" />
<GridViewColumn
Header="Name"
DisplayMemberBinding="{Binding Name}" />
<GridViewColumn
Header="Category"
DisplayMemberBinding="{Binding Category}" />
<GridViewColumn
Header="Price"
DisplayMemberBinding="{Binding Price}" />
</GridView>
</ListView.View>
<ListView />
when Test is an ICommand property in the view model.
But it's not responding to double click.
What am I getting wrong, and mabye there is another way?

WPF ListView Column width

I used list view with a single column, I would like this column with the same size list view with.
I mean Just one column fill the all with of the list view.
I tried this but didn't work...
<ListView Grid.Row="1" Grid.Column="1" x:Name="LvErrorList">
<ListView.View>
<GridView>
<GridViewColumn Width="{Binding ActualWidth, ElementName=LvErrorList}" Header="Status" DisplayMemberBinding="{Binding ErrorMessage}" />
</GridView>
</ListView.View>
</ListView>
I tried
<GridViewColumn Width="Auto" Header="Status" DisplayMemberBinding="{Binding ErrorMessage}" />
But some text are longer than the with of the ListView.
Becaues i don't know the user screen size i can't set max-width property.
Does anyone know how to fix this please?
You can try this way instead.
<ListView Grid.Row="1" Grid.Column="1" x:Name="LvErrorList">
<ListView.View>
<GridView>
<GridViewColumn Header="Status" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorMessage}" Width="{Binding ElementName=LvErrorList,Path=ActualWidth}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

Sorting the WPF Listview

Iam having a ListView named "ListLoginDetails" in WPF. In which I displays two columns namely "User_Name", "Password" through DataSet. Now I haven to Sort the ListView on Both Ascending & Descending Order. Based upon the button clicked.....
Home.Xaml:
<ListView Name="ListLoginDetails" ItemsSource="{Binding Path=Table}" Margin="18,0,0,0" Grid.RowSpan="3" >
<ListView.View>
<GridView x:Name="gridView">
<GridViewColumn Width="100" Header="User_Name" DisplayMemberBinding="{Binding Path=Id}" />
<GridViewColumn Width="140" Header="Password" DisplayMemberBinding="{Binding Path=Password}" />
</GridView>
</ListView.View>
</ListView>

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>

CheckBox state of a CellTemplate in 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

Resources