开发者

Mouse position in xaml - wpf

Is it possible to specify the position of the mouse as the value of the Parameter property of a Command in XAML. Something like the following:

<UserControl.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Create Link"
                      Command="{Binding CreateLink}"
                      CommandParameter="{Binding Mouse.Position}" >
            </M开发者_JAVA百科enuItem>
        </ContextMenu>
</UserControl.ContextMenu>


In WPF, there is no Mouse.Position. If you look at the Mouse class members, the closest thing is Mouse.GetPosition, which returns the position relative to some UI element.

I would recommend using the UI element itself as the Command Parameter, and having your Command call Mouse.GetPosition to retrieve positioning, if this is required in your DataContext for some reason.


Yeah, if you were looking to do it programatically, you can use the PointToScreen() as well as the Mouse.GetPosition(this) to get the absolute mouse point. Take a look at this should you need to do this programatically, not in XAML,

http://ivolo.mit.edu/post/WPF-Mouse-and-Point-Acrobatics.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜