-
Notifications
You must be signed in to change notification settings - Fork 461
Description
🐛 Bug Report
Maybe this code is a wrong approach to using FluentTreeView, but considering it responds to ObservableCollection changes (or is it the button click triggering a re-render), then this selection issue ideally should not happen.
Basically when you add an item from code behind, that is already Selected, initially everything looks okay, but selecting another item does not clear the selection from the just added item.
💻 Repro or Code Sample
Create a new Blazor Web App in Visual Studio.
Set render mode to @rendermode="new InteractiveServerRenderMode(false)"
Install FluentUI nuget package and perform the setup steps.
Use this code on home page to introduce the bug:
@page "/"
@using System.Collections.ObjectModel
<PageTitle>Home</PageTitle>
<FluentTreeView>
@foreach (var item in this.list)
{
<FluentTreeItem Text="@item.Name" Expanded="true">
@foreach (var subitem in item.TestItems)
{
<FluentTreeItem Text="@subitem.Name" @bind-Selected=subitem.IsSelected />
}
</FluentTreeItem>
}
</FluentTreeView>
<br />
<FluentButton Appearance="Appearance.Accent" OnClick="@onclick">Something</FluentButton>
@code
{
private ObservableCollection<Root> list = new ObservableCollection<Root>() { new Root() { Name = "root" } };
private void onclick(MouseEventArgs e)
{
this.list.First().TestItems.Add(new Test() { Name = "NewItem", IsSelected = true });
}
private class Root
{
public string Name { get; set; } = String.Empty;
public ObservableCollection<Test> TestItems = new ObservableCollection<Test>() { new Test() { Name = "test 1" }, new Test() { Name = "test 2" } };
}
private class Test
{
public string Name { get; set; } = String.Empty;
public bool IsSelected { get; set; }
}
}
Attached entire repo:
BlazorApp1.zip
🤔 Expected Behavior
After clicking the button, selecting another item should clear the selection from the added item.
😯 Current Behavior
🔦 Context
Learning Blazor from scratch, this type of code is used all over in tutorials and examples, to generate content dynamically.
Perhaps this type of code does not work in this context and one needs to use the FluentTreeView.Items property instead, for data binding and TreeItem generation.
Actually, thinking harder about the situation, it does make sense that this doesn't work, since the FluentTreeView has not been informed about a selection change, so internally it's logic to track selected items does not match what is rendered.
But perhaps there is something that can be improved here, either internal logic to handle this situation or some new method to inform the FluentTreeView of selection changes from code behind. Because we can't use the FluentTreeView.CurrentSelected nor the FluentTreeView.SelectedItem from code behind in this context, to select the desired item.
If this is deemed to be the wrong approach, then please update the documentation (https://www.fluentui-blazor.net/TreeView) with proper examples of data binding and generating contents from a data source. Currently there is nothing to go by, which would immitate real world scenarios.
PS! Using @bind-InitiallySelected=subItem.IsSelected or just InitiallySelected=@subItem.IsSelected instead of @bind-Selected=subitem.IsSelected, does not change the behavior.
Nor does implementing INotifyPropertyChanged for the IsSelected property.
🌍 Your Environment
- OS = Windows
- Browser = Microsoft Edge, Google Chrome
- .NET and Fluent UI Blazor library Version = 8.0.10 and 4.10.2
Visual Studio 2022 (17.11.5)
