1

我的列表视图(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);
            }
        }
    }     
4

1 回答 1

1

我找到了解决办法。我意识到,在加载完所有内容并选择了特定项目后,列表视图此时没有焦点。如果我按 TAB 键,焦点会跳转到列表视图中的选定项目。所以我以编程方式模拟了一个 TAB 按键事件 - 所以我的项目在加载窗口后获得焦点

于 2013-06-12T11:20:22.607 回答