如何在 DataGrid 行上处理鼠标?

How to handle a mouse over a DataGrid row?

任务是当鼠标指针悬停在一行(不考虑列)上时,更改DataGridTemplateColumn内元素的Visibility 属性。

<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>

是否可以在 UWP 中执行此操作,最好使用 MVVM?在 WPF 中,我可能会使用 DataTrigger(但 Style 没有 Triggers 属性)或绑定 属性 IsMouseOver,但 none 元素( DataGrid, DataGridTemplateColumn, RowStyle...)有这样一个属性.

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

当然,您可以使用 xaml behavior EventTriggerBehavior 来更改 DataGridTemplateColumn 内元素的可见性 属性 当鼠标指针悬停在一行上时。

例如

<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>