我的listview中有一个gridview(xaml) 我通过索引值在列表视图中设置所选项目。这很好用。
现在我希望键盘焦点位于所选项目上。
我可以将焦点设置到列表视图本身,但不能设置列表视图中的选定项目。
// 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);
}
}
}
答案 0 :(得分:1)
我找到了一个解决方法。我意识到在加载所有内容并选择特定项目后,列表视图此时没有焦点。如果我按TAB键,焦点将跳转到列表视图中的所选项目。所以我以编程方式模拟了一个TAB键按下事件 - 所以我的项目在加载窗口后获得焦点