我正在尝试找到一种显示文本块的方法,或者允许我任意拖放控件在屏幕上移动的方法。 我已经搜遍了谷歌和这里,但是我发现每一个与拖放有关的问题都是关于交换数据而不仅仅是位置。 有没有人知道准备好的东西,或者您可以指导我应该寻找什么方向?
<TextBlock Text="Hello!">
<i:Interaction.Behaviors>
<el:MouseDragElementBehavior ConstrainToParentBounds="True"/>
</i:Interaction.Behaviors>
</TextBlock>
您需要在解决方案中添加对 Microsoft.Expression.Interactions 的引用,并在 XAML 文件的顶部添加以下命名空间:
xmlns:el="clr-namespace:Microsoft.Expression.Interactivity.Layout;assembly=Microsoft.Expression.Interactions"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Height="30" Margin="125,132,0,0"
Name="textBlock1" Text="TextBlock"
Width="83" MouseMove="textBlock1_MouseMove" />
</Grid>
以及背后的代码:
private void textBlock1_MouseMove(object sender, MouseEventArgs e)
{
TextBlock realSender = (TextBlock)sender;
var theParent = (Grid)realSender.Parent;
var position = e.GetPosition(theParent);
realSender.Margin = new Thickness(
position.X - realSender.Width / 2,
position.Y - realSender.Height / 2, 0, 0);
}
var behavior = new MouseDragElementBehavior(); behavior.Attach(this.Label);
- Kevin Gosse