I have a gridview within my listview (xaml) I set the selected item in my listview by a index value. This works fine.
Now I want the keyboard focus to be on the selected item.
I'm able to set focus to the listview itself, but not to the selected item in the listview.
// Set selected item at index : i
listview1.SelectedIndex = i;
// Sets focus to listview
listview1.Focus();
// Try to set focus to selected item
ListViewItem myitem = listview1.Items[i] as ListViewItem;
myitem.Focus();
dgrid1.xaml
<ListView Name="listview1" >
<ListView.View>
<GridView ColumnHeaderToolTip="Multiple Category Information">
<GridViewColumn DisplayMemberBinding="{Binding Path=Enquiry_Number}" Header="Enquiry number"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Consignee_Ref}" Header="Consignee reference"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Booking_Reference}" Header="Booking reference"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Contract_Number}" Header="Contract Number"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Invoice_Number}" Header="Invoice Number"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Date_Invoice}" Header="Invoice Date"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Status}" Header="Status"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Period_Enquiry}" Header="Period Enquiry"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Consignee}" Header="Consignee"/>
</GridView>
</ListView.View>
</ListView>
dgrid1.xaml.cs
// Call the DataManager Class which will collect the data and fill the data set with it
DataManager.BindFilteredData(dts);
public static void BindFilteredData(DataSet dts)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
{
sql = "SELECT Enquiry_Number, Consignee_Ref, Booking_Reference, Contract_Number, Invoice_No, Date_Invoice, [Status], Period_Enquiry, Consignee FROM ConsHead";
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connection))
{
adapter.Fill(dts);
}
}
}
I found a work around. I realised that after everything is loaded and the specific item is selected, the listview does not have focus at this moment. If I press the TAB key the focus jumps to the selected item in the listview. So I Simulated a TAB key press event programatically - so my item gets focus after loading the window