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

ListBox display error #18264

Open
Linlccc opened this issue Feb 20, 2025 · 2 comments
Open

ListBox display error #18264

Linlccc opened this issue Feb 20, 2025 · 2 comments
Labels

Comments

@Linlccc
Copy link

Linlccc commented Feb 20, 2025

Describe the bug

After quickly scrolling up and down in the ListBox, some extra display items appear mysteriously, as shown in the figure, such as 80 and 86.

Image

To Reproduce

ViewModel

public class TestViewModel : ViewModelBase
{
    public class MyClass(string text, bool isImage)
    {
        public string Text { get; set; } = text;

        // Load random exception image
        public Bitmap? Image { get; set; } = isImage ? new Bitmap("/Users/lin/Pictures/截屏与录制/2484ed39-3cf6-4393-8dc4-fddfcee410b3.png") : null;
    }

    public ObservableCollection<MyClass> List { get; set; }
    public MyClass SelectedItem { get; set; }

    public TestViewModel()
    {
        List = [];
        for (int i = 0; i < 300; i++)
        {
            List.Add(new MyClass($"测试{i}", i % 3 == 0));
        }
        SelectedItem = List[0];
    }
}

Backend code

private void ClipItem_OnPointerEntered(object? sender, PointerEventArgs e)
{
    if (sender is Control { Parent: ListBoxItem listBoxItem }) listBoxItem.IsSelected = true;
}

View

<ListBox Grid.Row="1" IsVisible="True" ItemsSource="{Binding List}" SelectedItem="{Binding SelectedItem}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border Background="Transparent" PointerEntered="ClipItem_OnPointerEntered">
                <Grid ColumnDefinitions="*,Auto" Margin="9,3">
                    <StackPanel>
                        <TextBlock Text="{Binding Text}" FontSize="12" />
                        <Image Source="{Binding Image}" />
                    </StackPanel>
                    <ContentControl Grid.Column="1" x:Name="itemRight" />
                </Grid>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Expected behavior

No abnormal display occurs

Avalonia version

11.2.3

OS

macOS

Additional context

No response

@Linlccc Linlccc added the bug label Feb 20, 2025
@maxkatz6
Copy link
Member

maxkatz6 commented Feb 20, 2025

Does it reproduce without selection on pointer over?
You can try to change this line:

-    if (sender is Control { Parent: ListBoxItem listBoxItem }) listBoxItem.IsSelected = true;
+    if (sender is Control { Parent: ListBoxItem listBoxItem }) listBoxItem.SetCurrentValue(ListBoxItem.IsSelectedProperty, true);

@Linlccc
Copy link
Author

Linlccc commented Feb 20, 2025

Does it reproduce without selection on pointer over? You can try to change this line:

  • if (sender is Control { Parent: ListBoxItem listBoxItem }) listBoxItem.IsSelected = true;
  • if (sender is Control { Parent: ListBoxItem listBoxItem }) listBoxItem.SetCurrentValue(ListBoxItem.IsSelectedProperty, true);

The error display still occurs after the modification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants