2014-01-06 152 views
0

现在,我创建了一个MessageBox(一个自定义控件,不是用户控件)从Window继承,并为其提供ResourceDictionary中的样式。无法通过MouseLeftButtonDown拖动移动自定义消息框(自定义窗口)

像这样: enter image description here

我希望我可以将移动通过的MouseLeftButtonDown在MessageBox和移动Blue TitleBar,但它不符合我的方法工作。

这是我实现:

的.cs:

namespace Wpf.Controls 
{ 
    ... 
    public MessageBoxModule() 
    { 
     ... 
     SetupDragMoveCommand(); 
     ... 
    } 

    public static readonly DependencyProperty DragMoveCommandProperty = 
     DependencyProperty.Register(
      "DragMoveCommand", 
      typeof(RoutedCommand), 
      typeof(MessageBoxModule)); 

    public RoutedCommand DragMoveCommand 
    { 
     get { return (RoutedCommand)GetValue(DragMoveCommandProperty); } 
     set { SetValue(DragMoveCommandProperty, value); } 
    } 

    public void DragMoveCommandExecuted(object sender, ExecutedRoutedEventArgs e) 
    { 
     DragMove(); 
    } 

    private void SetupDragMoveCommand() 
    { 
     DragMoveCommand = new RoutedCommand(
      "DragMoveCommand", 
      typeof(MessageBoxModule)); 

     CommandBindings.Add(
      new CommandBinding(DragMoveCommand, DragMoveCommandExecuted)); 
    } 
    ... 
} 

.xmal:

<ResourceDictionary 
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    <Style TargetType="{x:Type local:MessageBoxModule}"> 
     <Setter Property="Template" > 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:MessageBoxModule}"> 
        <!--RootLayoutPanel--> 
        <DockPanel x:Name="RootLayoutPanel"> 
         <Grid x:Name="TopSiderLayout"> 
          <Rectangle x:Name="DragRectangle" Grid.ColumnSpan="2" StrokeThickness="0" Fill="#FF499A82" > 
           <i:Interaction.Triggers> 
            <i:EventTrigger EventName="MouseLeftButtonDown"> 
             <i:InvokeCommandAction Command="{TemplateBinding DragMoveCommand}"/ 
            </i:EventTrigger> 
           </i:Interaction.Triggers> 
          </Rectangle> 
... 

回答

0

现在,我仍然不知道为什么使用InvokeCommandAction到TemplateBinding命令,它是一个MessageBoxModule中定义的依赖项属性不起作用。

但我发现,使用TemplatePart这样一种方法:

的.cs:

public override void OnApplyTemplate() 
{ 
    base.OnApplyTemplate(); 

    Rectangle rect = GetTemplateChild("PART_TitleRectangle") as Rectangle; 
    if (rect != null) 
    { 
     rect.MouseLeftButtonDown += (sender, e) => { DragMove(); }; 
    } 
} 

的.xaml:

<Rectangle x:Name="PART_TitleRectangle" ... />