Domanda How to set the cell focus in WPF

  • giovedì 26 luglio 2012 05:21
     
      Contiene codice

    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

Tutte le risposte

  • giovedì 26 luglio 2012 12:13
     
      Contiene codice

    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

  • venerdì 27 luglio 2012 08:39
    Moderatore
     
     

    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

    • Contrassegnato come risposta Annabella LuoModerator martedì 7 agosto 2012 10:04
    • Contrassegno come risposta annullato sajithtgs mercoledì 8 agosto 2012 03:09
    •  
  • venerdì 27 luglio 2012 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

  • venerdì 27 luglio 2012 11:16
     
     
  • martedì 31 luglio 2012 09:17
     
     

    thanks Siva,

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

    Regards,

    sajith

  • venerdì 3 agosto 2012 11:18
    Moderatore
     
     

    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

  • lunedì 6 agosto 2012 03:51
     
     

    Thanks Annabella,

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

    Regards,

    sajith

  • giovedì 9 agosto 2012 00:59
    Moderatore
     
     

    Hi sajith,

    Do you still have any question on this?

    Best regards.


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

  • giovedì 9 agosto 2012 03: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

  • domenica 12 agosto 2012 15:43
     
      Contiene codice

    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





    • Proposto come risposta kishhr domenica 12 agosto 2012 15:43
    • Proposta come risposta annullata sajithtgs lunedì 13 agosto 2012 11:26
    •  
  • lunedì 13 agosto 2012 04: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