Domanda How to set the cell focus in WPF

  • 2012年7月26日 5:21
     
      包含代码

    Hi,

      Iam showing popup while clicking the cell of the grid.so while closing the popup the focus is not set to that cell.

    the issue is, iam setting the button inside the DataTemplate and  the popup click event  is handled by this button .

    the button will be a transparent.so while clicking the cell which has a button the focus will be set to that button not to the cell

    how can i set focus to the cell.

      <c:Column CellStyle="{StaticResource GridCommentStyle}"  x:Name="Comments" Binding="{Binding Comment}" ColumnName="PopUpComments" Header="Comments" MinWidth="100" IsReadOnly="True"  AllowResizing="False" Width="2*" >
                        <c:Column.CellTemplate>
                            <DataTemplate>
                                <Button Height="20"  Focusable="True" Content="{Binding Comment}" Style="{StaticResource TransparentButton}" Click="PopupClick" />
                            </DataTemplate>
                        </c:Column.CellTemplate>

    Regards,

    sajith

全部回复

  • 2012年7月26日 12:13
     
      包含代码

    Use this code:

    int index = 11;
    myDataGrid.SelectedItem = myDataGrid.Items[index];
    myDataGrid.ScrollIntoView(myDataGrid.Items[index]);
    DataGridRow dgrow = (DataGridRow)myDataGrid.ItemContainerGenerator.ContainerFromItem(myDataGrid.Items[index]);
    dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

    Or Refer this link:

    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/63974f4f-d9ee-45af-8499-42f29cbc22ae 


    Sivalingam

  • 2012年7月27日 8:39
    版主
     
     

    Hi sajith,

    Please refer to below link:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8f6011ba-f0c3-4c9f-ab72-1d15ee38123a/

    If you have any additional questions, please feel free to let me know.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

  • 2012年7月27日 10:36
     
     

    Many thanks Siva/Annabella

        Your post was helpful .

    what i need is to set the focus to the cell when the button is clicked  through Xaml .hope you are clear.

    here button is transparent so when we click on button inside the cell the focus actually goes to the button .not to the cell.

    Regards,

    sajith

  • 2012年7月27日 11:16
     
     
  • 2012年7月31日 9:17
     
     

    thanks Siva,

         What i need is to set focus to the cell when the button is clicked not to button.

    Regards,

    sajith

  • 2012年8月3日 11:18
    版主
     
     

    Hi sajithtgs,

    Thank you for your reply and sorry for my delay reply due.

    I thinking what you said for a long time and I think first you button is there in the datagrid cell, right? Event thoug it is transparent, but it is exist, and you need to click on it also, so how can we click on button and focus not on it? What we can do is to set the focus to cell after we click on button, right? And then we can change the focus in xaml also. So to set focus on cell in code is the way to go.

    Hope what I say is clear.

    And if you have any concern, please feel free to let me know.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

  • 2012年8月6日 3:51
     
     

    Thanks Annabella,

       Sorry my words was little confusing,Yes you are correct i exactly need what you said.

    Regards,

    sajith

  • 2012年8月9日 0:59
    版主
     
     

    Hi sajith,

    Do you still have any question on this?

    Best regards.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

  • 2012年8月9日 3:39
     
     

    Hi Annabella,

         I tried many ways to set the focus to the cell in the Xaml side.but i was not able to succeed.

    Regards,

    sajith

  • 2012年8月12日 15:43
     
      包含代码

    You should do in the below style:

      <c:Column CellStyle="{StaticResource GridCommentStyle}" 
    Focusable="{Binding IsFocused}"
    x:Name="Comments" Binding="{Binding Comment}" ColumnName="PopUpComments" Header="Comments" MinWidth="100" IsReadOnly="True" AllowResizing="False" Width="2*" > <c:Column.CellTemplate> <DataTemplate> <Button Height="20" Content="{Binding Comment}" Style="{StaticResource TransparentButton}" Click="PopupClick" /> </DataTemplate> </c:Column.CellTemplate>


    while closing the popup...

    ((this.Parent).Content as TestViewModel).IsFocused = true

    This solves your problem...

    _________________________________________________________________

    Do Vote the article and propose the same





    • 已建议为答案 kishhr 2012年8月12日 15:43
    • 取消建议作为答案 sajithtgs 2012年8月13日 11:26
    •  
  • 2012年8月13日 4:36
     
     

    thanks Kishhr,

       I tried your solution but it does not solve my issue.

    ((this.Parent).Content as TestViewModel).IsFocused = true,   here iam not getting the Content for my parent and also can u please

    ex-plane what do u mean by TestViewModel.

    Regards,

    sajith