简体   繁体   English

BindingList和ListBox行为

[英]BindingList and ListBox behaviour

I have a ListBox that is bound to a BindingList collection. 我有一个绑定到BindingList集合的ListBox。 This works great. 这很好。

My only grief occurs when the first item enters the collection. 当第一个项目进入收藏集时,我唯一的悲伤发生了。 The default behavior of the ListBox is to select that item - yet, this does not raise the SelectedIndexChanged event. ListBox的默认行为是选择该项目-但是,这不会引发SelectedIndexChanged事件。 I assume this is because the SelectedIndex is initially set to null; 我认为这是因为SelectedIndex最初设置为null。 on becoming anything but null, the index isn't actually changed; 在变为null以外的任何东西时,索引实际上并没有改变; rather assigned. 而是分配。 How can I stop the default behavior of selecting (highlighting) the first initial item added to the ListBox? 如何停止选择(突出显示)添加到列表框的第一个初始项的默认行为?

If my assumption is wrong, please shed some light? 如果我的假设是错误的,请说明一下?

Update 更新资料

Here is the core parts of my code thus far. 到目前为止,这是我代码的核心部分。

    public UavControlForm()
    {
        InitializeComponent();
        _controlFacade = new UavController.Facade.ControlFacade();
        UpdateFlightUavListBox();
    }

    private void UpdateFlightUavListBox()
    {
        lsbFlightUavs.DataSource = _controlFacade.GetFlightUavTally();
        lsbFlightUavs.DisplayMember = "Name";
    }

    private static BindingList<FlightUav> _flightUavTally = new BindingList<FlightUav>();
    public BindingList<FlightUav> FlightUavTally
    {
        get { return _flightUavTally; }
    }

    public void AddFlightUav(double[] latLonAndAlt)
    {
        FlightUav flightUav = new FlightUav();
        flightUav.Latitude = latLonAndAlt[0];
        flightUav.Longitude = latLonAndAlt[1];
        flightUav.Altitude = latLonAndAlt[2];
        _flightUavTally.Add(flightUav);

        UtilStk.InjectAircraftIntoStk(flightUav.Name);
        flightUav.SetFlightDefaults();
        PlayScenario();
    }

Update: 更新:

So, setting lsbFlightUavs.SelectedIndex = -1 solves the problem. 因此,设置lsbFlightUavs.SelectedIndex = -1可解决此问题。 The above method AddFlightUav() is called from a button's OnClick handler in a second form from the main form. 上面的方法AddFlightUav()是从按钮的OnClick处理程序中以第二种形式从主窗体中调用的。 How can I call lsbFlightUavs.SelectedIndex = -1 from this second form when the AddFlightUav() method returns? AddFlightUav()方法返回时,如何从第二种形式调用lsbFlightUavs.SelectedIndex = -1 I know I can make the ListBox static, but that seems like bad practice to me.. what would be a more elegant solution? 我知道我可以将ListBox设置为静态,但这对我来说似乎是一种坏习惯。

Using WinForms, I implemented the Singleton pattern. 使用WinForms,我实现了Singleton模式。 This enabled me to access the ListBox control from my second form. 这使我能够从第二个窗体访问ListBox控件。

Form 1 表格1

private static UavControlForm _instance = new UavControlForm();
private UavControlForm()
{
    InitializeComponent();   
}
public static UavControlForm Instance
{
    get { return _instance; }
}
public ListBox FlightUavListBox
{
    get { return lsbFlightUavs; }
}

Form 2 表格2

UavControlForm.Instance.FlightUavListBox.SelectedIndex = -1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM