如何将MouseDoubleClick-Event绑定到DataGridTemplateColumn

时间:2012-08-14 10:56:08

标签: wpf datagrid wpf-controls wpfdatagrid

您好我想将一个MouseDoubleClick事件用于我的DataGridTemplateColumn,用于H.Column下的Function-和Processinterface-Row。

但DataGridTemplateColumn没有MouseDoubleClick-Event。 任何人都可以帮忙!

这是我的代码:

   <DataGridTemplateColumn x:Name="" Header="H." Width="50">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding Path=ObjectType}" Value="E">
                                        <Setter Property="Visibility" Value="Hidden" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=ObjectType}" Value="Function">
                                        <Setter Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=ObjectType}" Value= "Processinterface">
                                        <Setter Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=ObjectType}" Value="AND">
                                        <Setter Property="Visibility" Value="Hidden" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=ObjectType}" Value="OR">
                                        <Setter Property="Visibility" Value="Hidden" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=ObjectType}" Value="XOR">
                                        <Setter Property="Visibility" Value="Hidden" />
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                                <Button Name="btnTable" Visibility="Hidden" Height="20" Width="25" Click="Button_Table_Click">
                                    <Image Height="16" Source="" Stretch="Fill" Width="16"/>
                                </Button>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

3 个答案:

答案 0 :(得分:0)

<DataGrid MouseDoubleClick="dataGridLog_MouseDoubleClick" ItemsSource="{Binding}" RowDetailsVisibilityMode="VisibleWhenSelected">
        <DataGrid.Columns>
            <DataGridTemplateColumn x:Name="dateColumn" Header="Date" Width="80" CanUserReorder="True" CanUserSort="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="dateColumnTextBlock" Text="{Binding Path=DateSent, StringFormat={}{0:dd/MM/yyyy}}" Width="80" />
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=DateSent, StringFormat={}{0:dd/MM/yyyy}, Converter={x:Reference Name=DateConverter}, ConverterParameter=>3}" Value="True">
                                <Setter TargetName="dateColumnTextBlock" Property="Background" Value="Red" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn x:Name="priorityColumn" Header="Priority" Width="80" CanUserReorder="True" CanUserSort="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="priorityColumnTextBlock" Text="{Binding Path=Priority}" Width="80" />
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=Priority}" Value="Urgent">
                                <Setter TargetName="priorityColumnTextBlock" Property="FontWeight" Value="Bold" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTextColumn x:Name="titleColumn" Binding="{Binding Path=Fault}" Header="Title" Width="270" CanUserReorder="True" CanUserSort="True" />
        </DataGrid.Columns>
    </DataGrid>

这基本上是我的眩光。我已经证明这一点可以证明它确实有MouseDoubleClick。此外,还展示了如何在模板上使用触发器。

答案 1 :(得分:0)

像这样写

<my:DataGrid 
            AutoGenerateColumns="False" 
            Margin="22,263,0,0" 
            Name="dgAcctSearch"  
            xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" 
            IsReadOnly="True" 
            CanUserResizeRows="False" 
            CanUserSortColumns="True" 
            SelectionMode="Extended" 
            SelectionUnit="Cell" 
            HorizontalAlignment="Left" 
            Width="783" 
            Height="331" 
            VerticalAlignment="Top" 
            MouseDoubleClick="dgAcctSearch_MouseDoubleClick" 
            CanUserAddRows="False" 
            CanUserDeleteRows="False"  
            CanUserReorderColumns="False" 
            CanUserResizeColumns="True" 
            AllowDrop="False"
            Background="WhiteSmoke"
            GridLinesVisibility="All"
            />

答案 2 :(得分:0)

然后在文件后面添加代码

private void dgAcctSearch_MouseDoubleClick(object sender, MouseButtonEventArgs e) { //add your logic here }