How to handle a mouse over a DataGrid row?

622 Views Asked by At

The task is to change Visibility property of element inside DataGridTemplateColumn when the mouse pointer is over a row (regardless of the column).

<controls:DataGridTemplateColumn>
    <controls:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock VerticalAlignment="Center"
                           Margin="12, 0, 0, 0"
                           Text="Mouse is not over" />

                <!-- Visible only if the mouse pointer is over a row -->
                <Button VerticalAlignment="Stretch"
                        HorizontalAlignment="Stretch">
                    Mouse is over
                </Button>
            </Grid>
        </DataTemplate>
    </controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>

Is it possible to do this in the UWP, preferably with MVVM? In WPF I would probably use a DataTrigger (but Style doesn't have Triggers property) or bind the property IsMouseOver, but none of the elements (DataGrid, DataGridTemplateColumn, RowStyle...) has a such property.

1

There are 1 best solutions below

1
On BEST ANSWER

The task is to change Visibility property of element inside DataGridTemplateColumn when the mouse pointer is over a row (regardless of the column).

Sure, you could use xaml behavior EventTriggerBehavior to change Visibility property of element inside DataGridTemplateColumn when the mouse pointer is over a row.

For example

<controls:DataGridTemplateColumn Header="Check" Tag="Test1">
    <controls:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Interactivity:Interaction.Behaviors>
                    <Interactions:EventTriggerBehavior EventName="PointerExited">
                        <Interactions:ChangePropertyAction
                            PropertyName="Visibility"
                            TargetObject="{Binding ElementName=Button1}"
                            Value="Collapsed" />
                    </Interactions:EventTriggerBehavior>

                    <Interactions:EventTriggerBehavior EventName="PointerEntered">
                        <Interactions:ChangePropertyAction
                            PropertyName="Visibility"
                            TargetObject="{Binding ElementName=Button1}"
                            Value="Visible" />
                    </Interactions:EventTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
                <TextBlock Text="{Binding Name}" />
                <Button
                    x:Name="Button1"
                    Content="test1"
                    Visibility="Collapsed" />
            </StackPanel>
        </DataTemplate>
    </controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>