Coder Social home page Coder Social logo

If SelectedItem is only bind and Items have the same name, selection is messed up on first focus about avaloniatokenizingtextbox HOT 7 CLOSED

puppetsw avatar puppetsw commented on July 22, 2024
If SelectedItem is only bind and Items have the same name, selection is messed up on first focus

from avaloniatokenizingtextbox.

Comments (7)

asyagelski avatar asyagelski commented on July 22, 2024 1

Just tested this. Using SelectionModel<string> bound to ListBox.Selection instead of binding to ListBox.SelectedItem directly seems to fix this issue.

Edit: Then you can bind to Selection.SelectedItem which returns "Test".

from avaloniatokenizingtextbox.

puppetsw avatar puppetsw commented on July 22, 2024

Something to do with the SelectedItem binding. Removing the binding in the view fixes the issue.
Issue occurs with normal ListBoxs from Avalonia.

View.xaml

<StackPanel>
   <ListBox Items="{Binding List1}" SelectedItem="{Binding SelectedItem}" />
   <ListBox Items="{Binding List1}" />
</StackPanel>

ViewModel.cs

public ObservableCollection<string> List1 { get; set; }
public string SelectedItem { get; set; }
public MainWindowViewModel()
{
  List1 = new ObservableCollection<string>();
  List1.Add("Test");
  List1.Add("Test");
  List1.Add("Test");
}

from avaloniatokenizingtextbox.

puppetsw avatar puppetsw commented on July 22, 2024

Interesting... thanks for taking the time to look at this.

Edit: I noticed stepping through the code, the SelectedIndex seems to be getting set twice in the SelectionModel. First time through the index is correct, but then it gets set to 0 for some reason.

from avaloniatokenizingtextbox.

asyagelski avatar asyagelski commented on July 22, 2024

Hm... I pulled it up again and unless I'm missing something or going crazy, I can't seem to replicate the issue we both noticed. I bound a TextBlock directly to ListBox.SelectedItem, I bound them both to a string property in the viewmodel, and I bound them both to a SelectionModel<string> object. They all behave correctly! Are you updated to the latest version of both Avalonia and the VisualStudio extension? Any ideas to reproduce the issue?

from avaloniatokenizingtextbox.

puppetsw avatar puppetsw commented on July 22, 2024

I don’t think I have the latest build. I’ll update and report back.

Edit: I just updated to 0.10.6 and the issue persists in my project. I might try a new one and see if it's still the same there.
Edit 2: I created a basic project, with just a ListBox, a ObservableCollection and string SelectedItem. The issue exists for me with just the Items and SelectedItem bound to the ListBox.

Stepping through the SelectionModel code. The SelectedItem is set correctly, until the SelectedIndex is set afterwards.

image

from avaloniatokenizingtextbox.

asyagelski avatar asyagelski commented on July 22, 2024

Hm... Is my setup any different from yours?
ListBoxShenanigans

from avaloniatokenizingtextbox.

puppetsw avatar puppetsw commented on July 22, 2024

I don't have the binding for SelectedIndex. If I add that to mine, it solves the issue. But the issue still persists for me if only the SelectedItem is bound to. The issue is only there while the SelectedItem on the ListBox is not null (so after the first click it works correctly)
Example1
image

I've been doing some more investigation on this. It appears that the line in Avalonia SelectionModel.cs might be causing the issue.

public T SelectedItem {
  get 
  {
    if (ItemsView is object) 
    {
      return GetItemAt(_selectedIndex);
    }
    else if (_initSelectedItems is object && _initSelectedItems.Count > 0) {
      return (T) _initSelectedItems[0];
    }

    return default;
  }
  set 
  {
    if (ItemsView is object) 
    {
      SelectedIndex = ItemsView.IndexOf(value ! ); //<----- This line here.
    }
    else 
    {
      Clear();#pragma warning disable CS8601
      SetInitSelectedItems(new T[] {
        value
      });#pragma warning restore CS8601
    }
  }
}

Commenting it out fixes the issue, but it makes the following tests fail

  • Focusing_Item_With_Ctrl_And_Arrow_Key_Should_Add_To_Selection
  • Focusing_Item_With_Ctrl_And_Arrow_Key_Should_Add_To_Selection
  • SelectedItem_Should_Not_Cause_StackOverflow

I might just make my control not allow duplicates, it would solve the issue without needing to figure out what's going on here.

The bug only happens when the List is bound to a collection of string, int, double, etc. collections of objects works as intended.

from avaloniatokenizingtextbox.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.