开发者

Setting the focus of listboxItem on pageload in Silverlight

开发者 https://www.devze.com 2022-12-23 17:16 出处:网络
I want to set focus on the first item of the listbox , on pageload. I can not use : listArtist.SelectedIndex=

I want to set focus on the first item of the listbox , on pageload.

I can not use :

listArtist.SelectedIndex=

as I navigate to another page on selected indexChanged.

I have the following code , but it is not working:

 void Event_Completed(object sender, RMSRes开发者_开发知识库ponseEventArgs e)
    {

        listArtist.ItemsSource = e.eOutData;
        listArtist.Focus();


    }


So finally I got a way to work it. First of all I had to remove the slectedIndex_Changed event , instead used key down event(On Enter Key Press) to navigate to another page.

Now what I did is , setting the focus of the List box on List Box loaded event. If You will try it from XAML it wont work as the itemssource is not set at that time. So my code is some thing similar as below:

    void Event_Completed(object sender, RMSResponseEventArgs e)
    {

        listArtist.ItemsSource = e.eOutData;

        listArtist.Loaded += new RoutedEventHandler(listbox_Loaded);

    }
   void listbox_Loaded(object sender, RoutedEventArgs e)
    {
        listArtist.SelectedIndex = 0;
        listArtist.Focus();
    }

Now if you are interested how I managed the slectedIndex_changed event then it goes as below:

private void listBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        // Your Code goes Here ;)

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消