2013-06-12 54 views
1

我在我的列表視圖(xaml) 內有一個gridview我通過索引值在我的列表視圖中設置選定的項目。這工作正常。集中選擇列表視圖

現在我想鍵盤焦點在選定的項目上。

我可以將焦點設置到列表視圖本身,但不能在列表視圖中選定的項目。

// 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

你不能使用listview1.Selecteditem.focus? – Terry

+1

listview1.SelectedItem.focus(); 這給出了「對象不包含'焦點'的定義和沒有擴展方法...等 –

+0

你可以顯示一些xaml(在列表視圖中嵌套的gridview)使我能夠複製你的問題 – Terry

回答

1

我發現周圍的工作。我意識到,在加載所有內容並選擇了特定項目之後,此時listview並沒有焦點。如果按下TAB鍵,焦點將跳轉到列表視圖中的選定項目。所以我模擬了一個TAB鍵按下事件編程 - 所以我的物品得到焦點後加載窗口