CommunityToolkit / CommunityToolkit/Maui

TouchBehavior error when attached to a Grid where there are multiple elements overlaying on the same row and some elements are initially not visible

Open
#1,911 3 comments 0 reactions 0 assignees View on GitHub
area/behaviors bug unverified
Dominant language
C#
Stars
2.7k
Forks
500
Avg merge
2d 14h
Merged PRs (30d)
7

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Did you read the "Reporting a bug" section on Contributing file?

- [X] I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug

### Current Behavior

It seems that attaching a TouchBehavior to a Grid, like in the next example, gives a problem when the visibility of an element that overlays another element on a row is changed from not visible to visible as a result of executing a touch command.

**MainPage.xaml:**
```











```
**MainViewModel.cs:**
```
public partial class MainViewModel: ObservableObject
{
[ObservableProperty]
ObservableCollection _items = new ObservableCollection();

[RelayCommand]
void SelectItem(ItemViewModel itemVM)
{
itemVM.IsSelected = !itemVM.IsSelected;
Debug.WriteLine($"SelectItem - Selected = {itemVM.IsSelected}");
}

public MainViewModel()
{
_items.Add(new ItemViewModel() { ThumbnailImageSource = "dotnet_bot.png"});
}
}
```
**ItemViewModel.cs:**
```
public partial class ItemViewModel: ObservableObject
{
[ObservableProperty]
bool _isSelected;

[ObservableProperty]
ImageSource _thumbnailImageSource;
}
```
What happens:
- The 1st time SelectItemCommand is executed it sets IsSelected on the item to true. As a result of this the IsVisible of the checked.png ImageSource is set to True but it doesn't display.
- The 2nd time the command executes IsSelected is set to False and IsVisible is set to False.
- The 3rd time the command executes IsVisible is again set to True and now the icon shows.

### Expected Behavior

TouchBehavior should not have impact on possible IsVisible properties of child elements of the element on which it is attached.

In this particular case the solution is to make all overlaying elements visible from the start and dynamically assign the ItemsSource depending on whether IsSelected is set, i.e. using a Converter, like so:

```




















```

### Steps To Reproduce

1. Open and run solution [mauiapp_touchbehavior_issue.zip](https://1drv.ms/u/s!AugQvFxkydtIirdJQ7c6wOiMsvSI-w?e=FFiplk)
2. Tap on the image.

### Link to public reproduction project repository

https://1drv.ms/u/s!AugQvFxkydtIirdJQ7c6wOiMsvSI-w?e=FFiplk

### Environment

```markdown
- .NET MAUI CommunityToolkit: 9.0.0
- OS: Windows 11
- .NET MAUI: 8.0
```

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.