Skip to content

Commit

Permalink
Changes for selected item dynamic binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Sofiya-kumar committed Apr 3, 2024
2 parents 0324907 + cdc4255 commit 220abca
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 192 deletions.
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>
11 changes: 1 addition & 10 deletions DemoApp/DemoApp/ViewModels/TMListViewPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ private void ItemSelected(SelectionChangedEventArgs e)
{
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]
Expand All @@ -81,7 +72,7 @@ private void SelectionGroup(TMRadioButtonEventArgs parameter)
}
else if (value == ListSelectionMode.Multiple)
{
SelectedItems = new ObservableCollection<object>() { ItemSource[5], ItemSource[6], };
SelectedItems = new ObservableCollection<object>() { ItemSource[2], ItemSource[3], };
}
}

Expand Down
208 changes: 99 additions & 109 deletions DemoApp/DemoApp/Views/ListViewSamplePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,118 +6,108 @@
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>
<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}"
SelectedItem="{Binding SelectedItem}"
SelectedItems="{Binding SelectedItems}">
</modus:TMListView>

</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"
ItemsSource="{Binding ItemSource}"
SelectionMode="{Binding SelectionMode}"
SelectionChangedCommand="{Binding ItemSelectedCommand}"
SelectedItem="{Binding SelectedItem}"
SelectedItems="{Binding SelectedItems}">
</modus:TMListView>

</StackLayout>
</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
33 changes: 25 additions & 8 deletions Trimble.Modus.Components/Controls/ListView/TMListView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Newtonsoft.Json;
using System.Collections;
using System.Windows.Input;
using Trimble.Modus.Components.Enums;
Expand Down Expand Up @@ -60,6 +61,11 @@ public new ListSelectionMode SelectionMode
public TMListView()
{
HasUnevenRows = true;
if (DeviceInfo.Platform == DevicePlatform.WinUI)
{
SetDynamicResource(BackgroundColorProperty, "CellDefaultBackgroundColor");
}
SeparatorVisibility = SeparatorVisibility.None;
ItemTapped += ListViewItemTapped;
(this as ListView)?.SetValue(ListView.SelectionModeProperty, ListViewSelectionMode.Single);
}
Expand All @@ -75,7 +81,9 @@ private void UpdateSelectedItemTappedValue(object newValue)
switch (SelectionMode)
{
case ListSelectionMode.Multiple:
var previousSelections = SelectedItems;
string serialized = JsonConvert.SerializeObject(SelectedItems);
var copy = JsonConvert.DeserializeObject<IList<object>>(serialized);
var previousSelections = copy;
if (SelectedItems.Contains(newValue))
{
SelectedItems.Remove(newValue);
Expand Down Expand Up @@ -174,38 +182,47 @@ private void UpdateCellColor()
{
foreach (var item in this.TemplatedItems)
{
Updated(item as ViewCell);
if (item is TextCell textCell)
{
Updated(textCell, TextCell.BackgroundColorProperty);
}
else if (item is TemplateCell templateCell)
{
Updated(templateCell, TemplateCell.BackgroundColorProperty);

}
}
}

private void Updated(ViewCell textCell)
private void Updated(ViewCell textCell, BindableProperty bindableProperty)
{
if (SelectionMode == ListSelectionMode.Single)
{
if (SelectedItem != null && SelectedItem.Equals(textCell.BindingContext))
{
textCell.SetDynamicResource(TextCell.BackgrondColorProperty, "CellSelectedBackgroundColor");
textCell.SetDynamicResource(bindableProperty, "CellSelectedBackgroundColor");
}
else
{
textCell.SetDynamicResource(TextCell.BackgrondColorProperty, "CellDefaultBackgroundColor");
textCell.SetDynamicResource(bindableProperty, "CellDefaultBackgroundColor");
}
}
else if (SelectionMode == ListSelectionMode.Multiple)
{
if (SelectedItems != null && SelectedItems.Contains(textCell.BindingContext))
{
textCell.SetDynamicResource(TextCell.BackgrondColorProperty, "CellSelectedBackgroundColor");
textCell.SetDynamicResource(bindableProperty, "CellSelectedBackgroundColor");
}
else
{
textCell.SetDynamicResource(TextCell.BackgrondColorProperty, "CellDefaultBackgroundColor");
textCell.SetDynamicResource(bindableProperty, "CellDefaultBackgroundColor");
}
}
else
{
textCell.SetDynamicResource(TextCell.BackgrondColorProperty, "CellDefaultBackgroundColor");
textCell.SetDynamicResource(bindableProperty, "CellDefaultBackgroundColor");
}
}

#endregion
}
Loading

0 comments on commit 220abca

Please sign in to comment.