从用户控件派生的WPF自定义控件不响应鼠标单击

时间:2010-09-28 03:59:16

标签: wpf

我有一个简单的自定义控件,它使用以下XAML派生自UserControl:

<UserControl x:Class="EMS.Controls.Dictionary.MapTip"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:slData="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
       xmlns:infrv="clr-namespace:OCC600.Infrastructure.Interface.Views;assembly=EMS.OCC600.Infrastructure.Interface"
        xmlns:igDP="http://infragistics.com/DataPresenter"       x:Name="root"                   
      > 

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Resources/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Viewbox x:Name="viewBox" >
        <Border CornerRadius="10" Background="#80000000" BorderBrush="Black">                 
            <Grid Name="contentGrid" Margin="0,20,0,0" >            
                <Grid.RowDefinitions>              
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>                   
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>                  
                </Grid.ColumnDefinitions>              

                <Border Grid.Row="2" Margin="5" Grid.ColumnSpan="2" Visibility="Visible" >                                                  
                    <igDP:XamDataGrid Margin="5"                                      
                                      Name="IdentifyDetailsDataGrid"     
                                      Style="{StaticResource readonlyGrid}"  
                                      ClipToBounds="False">                        
                        <igDP:XamDataGrid.FieldLayoutSettings>
                            <igDP:FieldLayoutSettings                         
                                AutoGenerateFields="True"                                                                                                                                   
                                HighlightAlternateRecords="True"                                
                                FilterUIType="LabelIcons"
                                AllowAddNew="False" 
                                AllowDelete="False"                                                                 
                                SelectionTypeCell="Single"                                
                                SelectionTypeField="Single"                        
                                />
                        </igDP:XamDataGrid.FieldLayoutSettings>
                        <igDP:XamDataGrid.FieldSettings>
                            <igDP:FieldSettings
                                LabelClickAction="SortByOneFieldOnly"                                 
                                AllowEdit="False" 
                                AllowGroupBy="True"                                 
                                CellClickAction="SelectRecord"                            
                                ExpandableFieldRecordHeaderDisplayMode="NeverDisplayHeader"/>
                        </igDP:XamDataGrid.FieldSettings>
                    </igDP:XamDataGrid>                    
                </Border>          
        </Grid>   
        </Border>
    </Viewbox>  
</UserControl>

这个Maptip的一个实例被添加到JSmith提供的DragCanvas中:http://www.codeproject.com/KB/WPF/DraggingElementsInCanvas.aspx

此控件是动态提供的工​​具栏,其中的按钮绑定到ViewModel中的命令。当我点击按钮时,没有任何反应。但是,如果我用System.Windows.Primitives.Popup类替换Maptip的基类,则当将此maptip添加到拖动画布时,按钮会响应鼠标单击事件事件并执行命令。

我在这里看到的任何暗示?

TIA。

1 个答案:

答案 0 :(得分:1)

必须使用自定义DragConvas重写OnPreviewMouseLeftButtonDown。更改为覆盖OnMouseLeftButtonDown而我的控件现在看到鼠标单击。从链接中获得了线索:

http://www.codeproject.com/KB/WPF/DraggingElementsInCanvas.aspx