Articles in this section
Category / Section

How to enable the button in GridTemplateColumn when mouse hovering in WPF DataGrid (SfDataGrid)?

2 mins read

You can show the button in template column, only when mouse over the row based on the IsMouseOver property of row control (VirtualizingCellsControl) in WPF DataGrid (SfDataGrid).

 

In the below code snippet, the visibility of button inside template column is changed based on the IsMouseOver property of VirtualizingCellsControl via converter.

XAML

<Window.Resources>
        <local:ViewModel x:Key="viewmodel"/>
        <local:BoolToVisibilityConverter x:Key="boolToVisibilityConverter"/>
</Window.Resources>
 
<!--Binding IsMouseOver property and Converter to the Visibility property of Button-->
 
<Syncfusion:GridTemplateColumn.CellTemplate>
     <DataTemplate>
          <Button Content="Click"     
                        Syncfusion:FocusManagerHelper.FocusedElement="True"  
                        Visibility="{Binding Path=IsMouseOver,             
                                          RelativeSource={RelativeSource FindAncestor, 
                                          AncestorType=Syncfusion:VirtualizingCellsControl},
                                          Converter={StaticResource boolToVisibilityConverter }}" >
           </Button>
      </DataTemplate>
</Syncfusion:GridTemplateColumn.CellTemplate>

 

 

C#

public class BoolToVisibilityConverter: IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((bool)value)
                return Visibility.Visible;
            return Visibility.Collapsed;
        }
 
        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }
    }

 

In the below screen shot, button visible only when mouse the row in DataGrid and for other rows its collapsed.

Enable the button in grid column when mouse hover in WPF DataGrid

View sample in GitHub.

Conclusion

I hope you enjoyed learning about how to enable the button in GridTemplateColumn when mouse hovering in WPF DataGrid (SfDataGrid).

You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Grid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied