Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

366 selecteditem binding is not working in list view #367

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions DemoApp/DemoApp/Platforms/Windows/App.xaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<maui:MauiWinUIApplication
x:Class="DemoApp.WinUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:maui="using:Microsoft.Maui"
xmlns:local="using:DemoApp.WinUI">
<maui:MauiWinUIApplication.Resources>
<StaticResource x:Key="ListViewItemBackgroundSelectedPointerOver" ResourceKey="ListViewItemBackgroundColor" />
<StaticResource x:Key="ListViewItemBackgroundSelected" ResourceKey="ListViewItemBackgroundColor" />
<SolidColorBrush x:Key="ListViewItemBackgroundColor" Color="#DCEDF9" />
<Thickness x:Key="TextControlBorderThemeThickness">0</Thickness>
<Thickness x:Key="TextControlBorderThemeThicknessFocused">0</Thickness>
</maui:MauiWinUIApplication.Resources>
<maui:MauiWinUIApplication x:Class="DemoApp.WinUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:maui="using:Microsoft.Maui"
xmlns:local="using:DemoApp.WinUI">
<maui:MauiWinUIApplication.Resources>
<StaticResource x:Key="ListViewItemBackgroundSelectedPointerOver"
ResourceKey="ListViewItemBackgroundColor" />
<StaticResource x:Key="ListViewItemBackgroundSelected"
ResourceKey="ListViewItemBackgroundColor" />
<SolidColorBrush x:Key="ListViewItemBackgroundColor"
Color="#DCEDF9" />
<Thickness x:Key="TextControlBorderThemeThickness">0</Thickness>
<Thickness x:Key="TextControlBorderThemeThicknessFocused">0</Thickness>
</maui:MauiWinUIApplication.Resources>
</maui:MauiWinUIApplication>
58 changes: 35 additions & 23 deletions DemoApp/DemoApp/ViewModels/TMListViewPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using CommunityToolkit.Mvvm.ComponentModel;
using DemoApp.Models;
using Newtonsoft.Json;
using Trimble.Modus.Components.Enums;
using System.Collections;
using CommunityToolkit.Mvvm.Input;
using Trimble.Modus.Components;
using DemoApp.Helper;
using DemoApp.Models;
using System.Collections.ObjectModel;
using Trimble.Modus.Components;
using Trimble.Modus.Components.Enums;
using SelectionChangedEventArgs = Trimble.Modus.Components.SelectionChangedEventArgs;
using DemoApp.Helper;

namespace DemoApp.ViewModels
{
Expand All @@ -18,7 +16,13 @@ internal partial class ListViewSamplePageViewModel : ObservableObject
private ListSelectionMode selectionMode;

[ObservableProperty]
private IEnumerable itemSource;
private ObservableCollection<User> itemSource;

[ObservableProperty]
private User selectedItem;

[ObservableProperty]
private ObservableCollection<object> selectedItems;
#endregion

#region Constructor
Expand All @@ -29,28 +33,23 @@ public ListViewSamplePageViewModel()
}

#endregion
#region Private Methods
#region Private Methods

private async void InitialzeUsers()
{
ItemSource = await UserDataCreator.LoadData();
}
ItemSource = await UserDataCreator.LoadData();
SelectionMode = ListSelectionMode.Single;
}

[RelayCommand]
private void ItemSelected(SelectionChangedEventArgs e)
{
if(e.PreviousSelection == null)
if (e.PreviousSelection == null)
{
Console.WriteLine("null");
}

foreach(var item in e.PreviousSelection)
{
Console.WriteLine(" PreviousSelections " + ((User)item).Name +" Index "+ e.SelectedIndex);
}
foreach (var item in e.CurrentSelection)
{
Console.WriteLine(" CurrentSelections " + ((User)item).Name + " Index " + e.SelectedIndex);
}
}
}
}

[RelayCommand]
private void SelectionGroup(TMRadioButtonEventArgs parameter)
{
Expand All @@ -63,7 +62,20 @@ private void SelectionGroup(TMRadioButtonEventArgs parameter)
_ => ListSelectionMode.Single,
};
}
}
}

partial void OnSelectionModeChanged(ListSelectionMode value)
{
if (value == ListSelectionMode.Single)
{
SelectedItem = ItemSource[0];
}
else if (value == ListSelectionMode.Multiple)
{
SelectedItems = new ObservableCollection<object>() { ItemSource[2], ItemSource[3], };
}
}

[RelayCommand]
private void PhoneClicked()
{
Expand Down
175 changes: 98 additions & 77 deletions DemoApp/DemoApp/Views/ListViewSamplePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,112 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DemoApp.Views.ListViewSamplePage"
xmlns:local ="clr-namespace:DemoApp.Views"
xmlns:local="clr-namespace:DemoApp.Views"
xmlns:constants="clr-namespace:DemoApp.Constant"
xmlns:modus="http://modus.trimble.com/components"
Title="ListViewSamplePage">
<ContentPage.Resources>
<DataTemplate x:Name="TextCell" x:Key="textCellKey">
<modus:TextCell
Title="{Binding Name}"
Description="{Binding Address}"
LeftIconSource="{Binding ProfilePic}"
RightIconSource="{Binding ProfilePic}">
</modus:TextCell>
</DataTemplate>
<DataTemplate x:Name="ViewCell" x:Key="viewCellKey">
<modus:TemplateCell>
<modus:TemplateCell.Content>
<Grid RowSpacing="10" Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ContentPage.Resources>
<DataTemplate x:Name="TextCell"
x:Key="textCellKey">
<modus:TextCell Title="{Binding Name}"
Description="{Binding Address}"
LeftIconSource="{Binding ProfilePic}"
RightIconSource="{Binding ProfilePic}">
</modus:TextCell>
</DataTemplate>
<DataTemplate x:Name="ViewCell"
x:Key="viewCellKey">
<modus:TemplateCell>
<modus:TemplateCell.Content>
<Grid RowSpacing="10"
RowDefinitions="auto,auto,auto,auto"
ColumnDefinitions="auto,*,*"
Padding="10">
<Border HeightRequest="80"
WidthRequest="80"
VerticalOptions="Center"
Stroke="{Binding Color}"
StrokeShape="RoundRectangle 40"
StrokeThickness="8"
Grid.RowSpan="3"
Margin="0,0,0,0">
<Image Source="{Binding ProfilePic}"
Margin="10,0,10,0"
VerticalOptions="Center"
HorizontalOptions="Center"
HeightRequest="80"
WidthRequest="80" />

<Border HeightRequest="80"
WidthRequest="80"
VerticalOptions="Center"
Stroke="{Binding Color}"
StrokeShape="RoundRectangle 40"
StrokeThickness="8"
Grid.RowSpan="3"
Margin="0,0,0,0">
<Image Source="{Binding ProfilePic}"
Margin="10,0,10,0"
VerticalOptions="Center" HorizontalOptions="Center" HeightRequest="80" WidthRequest="80"/>
</Border>

</Border>
<Label Text="{Binding Name}"
Grid.Row="0"
Grid.Column="1"
FontSize="Medium" />
<Label Text="{Binding DateofBirth, StringFormat='DateofBirth: {0:dd/MM/yyyy}'}"
Grid.Row="1"
Grid.Column="1"
FontSize="Small" />
<Label Text="{Binding Address}"
Grid.Row="2"
Grid.Column="1"
FontSize="Micro" />
<StackLayout Grid.Column="2"
Grid.Row="1"
HorizontalOptions="Center"
Orientation="Horizontal"
Spacing="5"
VerticalOptions="Center">
<ImageButton Source="{x:Static constants:ImageConstants.Email}"
HeightRequest="20"
WidthRequest="20"
Command="{Binding EmailClickedCommand}" />
<ImageButton Source="{x:Static constants:ImageConstants.Phone}"
HeightRequest="20"
WidthRequest="20"
Command="{Binding PhoneClickedCommand}" />
</StackLayout>

<Label Text="{Binding Name}" Grid.Row="0" Grid.Column="1" FontSize="Medium"/>
<Label Text="{Binding DateofBirth, StringFormat='DateofBirth: {0:dd/MM/yyyy}'}" Grid.Row="1" Grid.Column="1" FontSize="Small"/>
<Label Text="{Binding Address}" Grid.Row="2" Grid.Column="1" FontSize="Micro"/>
<StackLayout Grid.Column="2" Grid.Row="1" HorizontalOptions="Center" Orientation="Horizontal" Spacing="5" VerticalOptions="Center" >
<ImageButton Source="{x:Static constants:ImageConstants.Email}" HeightRequest="20" WidthRequest="20" Command="{Binding EmailClickedCommand}"/>
<ImageButton Source="{x:Static constants:ImageConstants.Phone}" HeightRequest="20" WidthRequest="20" Command="{Binding PhoneClickedCommand}"/>
</StackLayout>
</Grid>
</modus:TemplateCell.Content>
</modus:TemplateCell>
</DataTemplate>

</Grid>
</modus:TemplateCell.Content>
</modus:TemplateCell>
</DataTemplate>

</ContentPage.Resources>
</ContentPage.Resources>
<StackLayout Orientation="Vertical">
<Label Text="Select View Type" FontSize="Medium" Padding="10"></Label>
<modus:TMRadioButtonGroup x:Name="CellGroup"
Padding="10"
SelectedIndex="0"
HorizontalOptions="Center"
SelectedRadioButtonChanged="OnCellGroupButtonChanged"
Orientation="Horizontal">
<modus:TMRadioButton Text="Text Cell" />
<modus:TMRadioButton Text="Template Cell" />
</modus:TMRadioButtonGroup>
<Label Text="Selection Mode" FontSize="Medium" Padding="10"></Label>
<modus:TMRadioButtonGroup x:Name="SelectionGroup"
Padding="10"
SelectedIndex="0"
HorizontalOptions="Center"
SelectedRadioButtonChangedCommand="{Binding SelectionGroupCommand}"
Orientation="Horizontal">
<modus:TMRadioButton Text="Single" />
<modus:TMRadioButton Text="Multiple" />
<modus:TMRadioButton Text="None" />
</modus:TMRadioButtonGroup>
<modus:TMListView x:Name="textCellList"
IsVisible="True"
SelectionMode="{Binding SelectionMode}"
SelectionChangedCommand="{Binding ItemSelectedCommand}"
ItemsSource="{Binding ItemSource}">
</modus:TMListView>
<Label Text="Select View Type"
FontSize="Medium"
Padding="10"></Label>
<modus:TMRadioButtonGroup x:Name="CellGroup"
Padding="10"
SelectedIndex="0"
HorizontalOptions="Center"
SelectedRadioButtonChanged="OnCellGroupButtonChanged"
Orientation="Horizontal">
<modus:TMRadioButton Text="Text Cell" />
<modus:TMRadioButton Text="Template Cell" />
</modus:TMRadioButtonGroup>
<Label Text="Selection Mode"
FontSize="Medium"
Padding="10"></Label>
<modus:TMRadioButtonGroup x:Name="SelectionGroup"
Padding="10"
SelectedIndex="0"
HorizontalOptions="Center"
SelectedRadioButtonChangedCommand="{Binding SelectionGroupCommand}"
Orientation="Horizontal">
<modus:TMRadioButton Text="Single" />
<modus:TMRadioButton Text="Multiple" />
<modus:TMRadioButton Text="None" />
</modus:TMRadioButtonGroup>
<modus:TMListView x:Name="textCellList"
IsVisible="True"
SelectionMode="{Binding SelectionMode}"
SelectionChangedCommand="{Binding ItemSelectedCommand}"
ItemsSource="{Binding ItemSource}"
SelectedItem="{Binding SelectedItem}"
SelectedItems="{Binding SelectedItems}">
</modus:TMListView>

</StackLayout>
</ContentPage>
11 changes: 7 additions & 4 deletions DemoApp/DemoApp/Views/ListViewSamplePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ public ListViewSamplePage()
#region Private Methods
private void OnCellGroupButtonChanged(object sender, TMRadioButtonEventArgs e)
{
textCellList.ItemTemplate = e.RadioButtonIndex switch
if (e.RadioButtonIndex == 0)
{
1 => ViewCell,
_ => TextCell
};
textCellList.ItemTemplate = TextCell;
}
else
{
textCellList.ItemTemplate = ViewCell;
}
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Trimble.Modus.Components;
public class SelectionChangedEventArgs : EventArgs
{
public IReadOnlyList<object> PreviousSelection { get; set; }
public IReadOnlyList<object> CurrentSelection { get; set; }
public object PreviousSelection { get; set; }
public object CurrentSelection { get; set; }
public int SelectedIndex { get; set; }
}

Loading
Loading