代码之家  ›  专栏  ›  技术社区  ›  iLemming

如何区分鼠标点击和控件的不同部分

  •  0
  • iLemming  · 技术社区  · 14 年前

    如果我有这样的模板控件:

     <Style x:Key="HomeButtonStyle" TargetType="{x:Type Control}" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel VerticalAlignment="Top">
                        <Rectangle Width="20" Height="50" x:Name="PART_Rectangle" />
                        <ed:RegularPolygon x:Name="PART_Triangle" PointCount="3" 
                               Height="8" >
                        </ed:RegularPolygon>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    现在,如何在PART\u矩形单击时触发某种事件?那这和点击第三部分的三角形有什么区别呢?

    1 回复  |  直到 14 年前
        1
  •  0
  •   iLemming    14 年前

    哦。。。我知道了。例如:

         private void Control_MouseDown(object sender, MouseButtonEventArgs e)
        {
           if( ((FrameworkElement)(e.OriginalSource)).Name == "PART_Rectangle")
           {
               //RectangleMouseDown 
          }
        }