0

如何在 DatagridTemplateColumn 上添加 MouseDoubleClick 事件?

我的专栏:

<DataGridTemplateColumn Header="PK"> 
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Name="pk" HorizontalAlignment="Center" Stretch="None" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=Primary_Key}" Value="J">
    <Setter TargetName="pk" Property="Source" Value="/UserInterface;component/Resources/Images/key.png"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我现在使用 DataGrid 本身上的 MouseDoubleClick 事件来执行此操作,但是每次双击任何单元格时都会触发此offcourse:

Private Sub dgColumns_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
    Dim dg As DataGrid = CType(sender, DataGrid)
    If dg.SelectedItem IsNot Nothing AndAlso dg.SelectedItem.GetType Is GetType(Attribuut) Then
        If CType(dg.SelectedItem, Attribuut).Primary_Key = "J" Then
            CType(dg.SelectedItem, Attribuut).Primary_Key = "N"
        Else
            CType(dg.SelectedItem, Attribuut).Primary_Key = "J"
        End If
    End If
End Sub

这会使图像列发生变化,但只有当我离开单元格时,我将如何立即执行此操作?

4

1 回答 1

1

我建议将您的图像包装在 ContentControl 中。ContentControl 是 Control 的子类。MouseDoubleClick 在 Control 类中定义。

<ContentControl MouseDoubleClick="cc_MouseDoubleClick">
    <Image Name="pk" Source="my.png" HorizontalAlignment="Center" Stretch="None" />
</ContentControl>
于 2011-04-29T10:37:37.003 回答