DatagridTemplateColumn上的MouseDoubleClick事件

时间:2011-04-29 08:14:42

标签: wpf vb.net xaml double-click datagridtemplatecolumn

如何在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

这会使列Image发生变化,但只有当我离开单元格时,我才能立即执行此操作?

1 个答案:

答案 0 :(得分:1)

我建议将你的Image包装在ContentControl中。 ContentControl是Control的子类。 MouseDoubleClick在Control类中定义。

<ContentControl MouseDoubleClick="cc_MouseDoubleClick">
    <Image Name="pk" Source="my.png" HorizontalAlignment="Center" Stretch="None" />
</ContentControl>