none
place a button On top of datagrid selected row/cell RRS feed

  • Question

  • Dear All ,

    how can i get DataGrid selected row potion/coordination ?

    i need to place a button on top of DataGrid selected row , not In the dataGrid row (using cell template )

    thanks


    Tuesday, December 1, 2015 8:28 AM

Answers

  • If it has to be bigger than the parent row then you could put the button in an adorner layer.

    The adorner layer goes on top of everything else in the window but you can associate an adorner with a control so it will move with it.

    This sample does something rather different with an adorner, you'd want a button rather than listbox and obviously it would be positioned on top of your row:

    https://gallery.technet.microsoft.com/WPF-Adorner-PopUp-Sample-f31552ad

    As you will see, there's a fair bit of code but it'd be mostly cut and paste.

    .

    If you don't fancy that then you could try binding FocusManager.FocusedElement.

    See if that's in a datagridrow and use the co-ordinates to move a button on top.

    Point pt = dgrow.TransformToAncestor(rootVisual)
               .Transform(new Point(0, 0));
    Of course, it could well move which is why the adorner approach is probably easier.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by Hani Safa Wednesday, December 2, 2015 7:59 AM
    Tuesday, December 1, 2015 11:20 AM
    Moderator
  • >>how can i get DataGrid selected row potion/coordination ?

    If you put the DataGrid in a Canvas you could get a specific DataGridRow elements' relative position to the Canvas and add an element to the Canvas at this position like this:

                DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(1) as DataGridRow; //second row
                Point relativeLocation = row.TranslatePoint(new Point(0, 0), canvas);
    
                //add button on top of the row:
                Button btn = new Button() { Content = "..." };
                Canvas.SetLeft(btn, relativeLocation.X);
                Canvas.SetTop(btn, relativeLocation.Y);
                canvas.Children.Add(btn);
    

            <Canvas x:Name="canvas">
                <DataGrid x:Name="dataGrid">
                    <DataGrid.Columns>
                        ...
                </DataGrid>
            </Canvas>

    The Canvas is the only built-in panel control where you can explicitly position child elements by using coordinates that are relative to the Canvas area.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Hani Safa Wednesday, December 2, 2015 7:59 AM
    Tuesday, December 1, 2015 4:28 PM

All replies

  • Why is the answer not in the datagrid row?

    Because it seems to me that a button filling the row =  a button on top of the row.

                <DataGrid.RowStyle>
                    <Style TargetType="{x:Type DataGridRow}">
                        <Style.Triggers>
                            <Trigger Property="IsSelected"  Value="True">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate>
                                            <Button Content="Click Me"/>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            </Style.Triggers>
                         </Style>
    If it's supposed to just go in a cell then a templatecolumn with edit template showing the button.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, December 1, 2015 10:43 AM
    Moderator
  • Dear Andy

    for the same reason in my previous post ,  bind a ScrollBar to a datagrid scroll ,

    the desinger need the button to be grater than the row size

    the attached

    Tuesday, December 1, 2015 10:57 AM
  • If it has to be bigger than the parent row then you could put the button in an adorner layer.

    The adorner layer goes on top of everything else in the window but you can associate an adorner with a control so it will move with it.

    This sample does something rather different with an adorner, you'd want a button rather than listbox and obviously it would be positioned on top of your row:

    https://gallery.technet.microsoft.com/WPF-Adorner-PopUp-Sample-f31552ad

    As you will see, there's a fair bit of code but it'd be mostly cut and paste.

    .

    If you don't fancy that then you could try binding FocusManager.FocusedElement.

    See if that's in a datagridrow and use the co-ordinates to move a button on top.

    Point pt = dgrow.TransformToAncestor(rootVisual)
               .Transform(new Point(0, 0));
    Of course, it could well move which is why the adorner approach is probably easier.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by Hani Safa Wednesday, December 2, 2015 7:59 AM
    Tuesday, December 1, 2015 11:20 AM
    Moderator
  • >>how can i get DataGrid selected row potion/coordination ?

    If you put the DataGrid in a Canvas you could get a specific DataGridRow elements' relative position to the Canvas and add an element to the Canvas at this position like this:

                DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(1) as DataGridRow; //second row
                Point relativeLocation = row.TranslatePoint(new Point(0, 0), canvas);
    
                //add button on top of the row:
                Button btn = new Button() { Content = "..." };
                Canvas.SetLeft(btn, relativeLocation.X);
                Canvas.SetTop(btn, relativeLocation.Y);
                canvas.Children.Add(btn);
    

            <Canvas x:Name="canvas">
                <DataGrid x:Name="dataGrid">
                    <DataGrid.Columns>
                        ...
                </DataGrid>
            </Canvas>

    The Canvas is the only built-in panel control where you can explicitly position child elements by using coordinates that are relative to the Canvas area.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Hani Safa Wednesday, December 2, 2015 7:59 AM
    Tuesday, December 1, 2015 4:28 PM
  • >>how can i get DataGrid selected row potion/coordination ?

    If you put the DataGrid in a Canvas you could get a specific DataGridRow elements' relative position to the Canvas and add an element to the Canvas at this position like this:

                DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(1) as DataGridRow; //second row
                Point relativeLocation = row.TranslatePoint(new Point(0, 0), canvas);
    
                //add button on top of the row:
                Button btn = new Button() { Content = "..." };
                Canvas.SetLeft(btn, relativeLocation.X);
                Canvas.SetTop(btn, relativeLocation.Y);
                canvas.Children.Add(btn);

            <Canvas x:Name="canvas">
                <DataGrid x:Name="dataGrid">
                    <DataGrid.Columns>
                        ...
                </DataGrid>
            </Canvas>

    The Canvas is the only built-in panel control where you can explicitly position child elements by using coordinates that are relative to the Canvas area.

    Only if you ignore margin on something inside a grid.

    The fly in the ointment with positioning a separate button is the movement of the datagridrow.

    Which is why I think an adorner with a button in it is a better solution.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, December 1, 2015 4:39 PM
    Moderator
  • thanks a lot for your help ,

    for quick demo with no designer interactive Magnus solution worked fine ,

    when move to production , your solution is the way

    regards

    Wednesday, December 2, 2015 8:02 AM