现在,我正在创建一个从Window继承的MessageBox(一个自定义控件,而不是一个用户控件),并为它提供一个ResourceDictionary样式。
像这样:
我希望我可以通过MouseLeftButtonDown拖动移动消息框并移动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 :(得分: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" ... />