<bdo id='mP9Sg'></bdo><ul id='mP9Sg'></ul>
  1. <small id='mP9Sg'></small><noframes id='mP9Sg'>

    <legend id='mP9Sg'><style id='mP9Sg'><dir id='mP9Sg'><q id='mP9Sg'></q></dir></style></legend>
    <tfoot id='mP9Sg'></tfoot>
    1. <i id='mP9Sg'><tr id='mP9Sg'><dt id='mP9Sg'><q id='mP9Sg'><span id='mP9Sg'><b id='mP9Sg'><form id='mP9Sg'><ins id='mP9Sg'></ins><ul id='mP9Sg'></ul><sub id='mP9Sg'></sub></form><legend id='mP9Sg'></legend><bdo id='mP9Sg'><pre id='mP9Sg'><center id='mP9Sg'></center></pre></bdo></b><th id='mP9Sg'></th></span></q></dt></tr></i><div id='mP9Sg'><tfoot id='mP9Sg'></tfoot><dl id='mP9Sg'><fieldset id='mP9Sg'></fieldset></dl></div>
    2. ListBox.SelectedIndexChanged 第一次没有触发

      时间:2023-10-07
          <bdo id='qWJyT'></bdo><ul id='qWJyT'></ul>

          <legend id='qWJyT'><style id='qWJyT'><dir id='qWJyT'><q id='qWJyT'></q></dir></style></legend>

          <i id='qWJyT'><tr id='qWJyT'><dt id='qWJyT'><q id='qWJyT'><span id='qWJyT'><b id='qWJyT'><form id='qWJyT'><ins id='qWJyT'></ins><ul id='qWJyT'></ul><sub id='qWJyT'></sub></form><legend id='qWJyT'></legend><bdo id='qWJyT'><pre id='qWJyT'><center id='qWJyT'></center></pre></bdo></b><th id='qWJyT'></th></span></q></dt></tr></i><div id='qWJyT'><tfoot id='qWJyT'></tfoot><dl id='qWJyT'><fieldset id='qWJyT'></fieldset></dl></div>
        • <small id='qWJyT'></small><noframes id='qWJyT'>

            <tbody id='qWJyT'></tbody>
          <tfoot id='qWJyT'></tfoot>

              1. 本文介绍了ListBox.SelectedIndexChanged 第一次没有触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 ListBox 绑定到一个默认为空的 BindingList.
                当所选索引发生更改时,它应该使用来自所选对象的数据更新其他控件.
                问题是 SelectedIndexChanged 事件未在第一个条目上触发(索引从 -1 更改为 0).
                但是,当我再次单击第一个条目(尽管在这种情况下索引没有改变)以及添加更多条目时,它确实会触发.
                我检查了属性 myListBox.SelectedIndex ,它实际上从 -1 更改为 0,但由于某种原因没有调用事件处理程序.有谁知道它为什么这样做以及如何解决它?

                I have a ListBox bound to a BindingList which is empty by default.
                When the selected index changes it's supposed to update other controls with data from the selected object.
                The problem is that the SelectedIndexChanged event is not firing on the first entry (index changing from -1 to 0).
                It does fire however when I'm clicking on the first entry again (although index is not changing in this case) and when adding any more entries.
                I checked the property myListBox.SelectedIndex and it does in fact change from -1 to 0 but for some reason doesn't call the event handler. Does anyone know why it's doing this and how to fix it?

                这是我的代码:

                public partial class main : Form
                {
                    // The class of objects in my BindingList
                    [Serializable]
                    public class DisplayDefinition : INotifyPropertyChanged
                    {
                        private string _name;
                        private int _width, _height, _posx, _posy;
                
                        public string Name { get { return _name; } set { _name = value; NotifyPropertyChanged("Name"); } }
                        public int Width { get { return _width; } set { _width = value; NotifyPropertyChanged("Width"); } }
                        public int Height { get { return _height; } set { _height = value; NotifyPropertyChanged("Height"); } }
                
                        public event PropertyChangedEventHandler PropertyChanged;
                        private void NotifyPropertyChanged(string s)
                        {
                            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(s));
                        }
                    }
                
                    // Defining the BindingList 
                    BindingList<DisplayDefinition> displaydefinitions = new BindingList<DisplayDefinition>();
                
                    // Binding the list to my listbox 
                    public main()
                    {
                        InitializeComponent();
                        listDisplays.DataSource = displaydefinitions;
                        listDisplays.DisplayMember = "Name";
                    }
                
                    // Button adding a new object to the list
                    private void btnNewDisplay_Click(object sender, EventArgs e)
                    {
                        DisplayDefinition d = new DisplayDefinition();
                        displaydefinitions.Add(d);
                        listDisplays.SelectedItem = d;
                    }
                
                    private void listDisplays_SelectedIndexChanged(object sender, EventArgs e)
                    {
                        DisplayDefinition d = (DisplayDefinition)listDisplays.SelectedItem;
                        // Do something with "d" ...
                    }
                }
                

                推荐答案

                问题:

                此行为仅在将 ListBox 与数据源一起使用时发生,而在手动填充 ListBox 时不会发生.

                The problem:

                This behavior only occurs when using the ListBox with a data source, and doesn't occur when filling the ListBox manually.

                将第一项添加到数据源时,默认情况下会选择第一项而不会触发 SelectedIndexChanged 事件(不知道为什么!)成为 ListBox 中的错误,这使得设置 SelectedItemSelectedIndex 属性无用.

                When adding the first item to the data source, the first item gets selected by default without firing the SelectedIndexChanged event (not sure why!) which seems to be a bug in ListBox, and that makes setting the SelectedItem or SelectedIndex property useless.

                您可以在设置实际 SelectedIndex/SelectedItem 之前将 SelectedIndex 属性更改为临时索引 (-1) 以触发SelectedIndexChanged 事件.

                You can change the SelectedIndex property to a temporary index (-1) before setting the actual SelectedIndex/SelectedItem in order to trigger the SelectedIndexChanged event.

                应该可以使用以下方法:

                // Button adding a new object to the list
                private void btnNewDisplay_Click(object sender, EventArgs e)
                {
                    DisplayDefinition d = new DisplayDefinition();
                    d.Name = "SomeName";
                    displaydefinitions.Add(d);
                    listDisplays.SelectedIndex = -1;
                    listDisplays.SelectedItem = d;
                }
                
                private void listDisplays_SelectedIndexChanged(object sender, EventArgs e)
                {
                    if (listDisplays.SelectedIndex == -1) return;
                    DisplayDefinition d = (DisplayDefinition)listDisplays.SelectedItem;
                    // Do something with "d" ...
                    Console.WriteLine(d.Name);
                }
                

                希望有帮助:)

                这篇关于ListBox.SelectedIndexChanged 第一次没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何更改列表框中单词的颜色 下一篇:在使用 ItemsSource 之前,项目集合必须为空

                相关文章

                <i id='KDhmc'><tr id='KDhmc'><dt id='KDhmc'><q id='KDhmc'><span id='KDhmc'><b id='KDhmc'><form id='KDhmc'><ins id='KDhmc'></ins><ul id='KDhmc'></ul><sub id='KDhmc'></sub></form><legend id='KDhmc'></legend><bdo id='KDhmc'><pre id='KDhmc'><center id='KDhmc'></center></pre></bdo></b><th id='KDhmc'></th></span></q></dt></tr></i><div id='KDhmc'><tfoot id='KDhmc'></tfoot><dl id='KDhmc'><fieldset id='KDhmc'></fieldset></dl></div>
              2. <tfoot id='KDhmc'></tfoot>

                <small id='KDhmc'></small><noframes id='KDhmc'>

                  <bdo id='KDhmc'></bdo><ul id='KDhmc'></ul>

                    <legend id='KDhmc'><style id='KDhmc'><dir id='KDhmc'><q id='KDhmc'></q></dir></style></legend>