【问题标题】:Double clickable transparent panel in wpfwpf中的双击透明面板
【发布时间】:2019-03-25 02:12:09
【问题描述】:

我正在使用 VLC 插件编写 MediaPlayer,并希望使 vlc (WindowsFormsintegration) 可双击(全屏)。但是没有双击事件。所以我想到了一个覆盖 VLC (WFI) 的可点击透明面板。有人可以帮助我吗?由于缺乏透明度,我在 Windows 上使用 WPF 并将 Winforms 转换为 WPF。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    在 WPF 中将一个透明项目放在另一个项目上很容易。下面的示例 WPF 显示了覆盖 Grid 控件上的按钮的透明边框。

    <Grid Background="#006000">
        <Button Content="clicky" Click="clicky"
                HorizontalAlignment="Left" Margin="30"/>
        <Border Background="#00FFFFFF" >
            <Border.InputBindings>
                <MouseBinding MouseAction="LeftDoubleClick "
                    Command="{Binding CommandDoubleClick}"/>
            </Border.InputBindings>
        </Border>
    </Grid>     
    

    Border 元素绑定了一个双击命令。

    这样做的问题是覆盖Button 的透明Border 会阻止Button 被点击。您不能通过单击 Border 元素来访问它下面的元素。如果您不需要访问透明元素下的任何内容,那么这应该适合您。如果你确实需要访问透明项下的元素,那么这根本行不通。

    下图为边框双击事件绑定的RelayCommand代码及相关方法:

        private bool CanDoubleClick = true;
        RelayCommand commandDoubleClick;
        public ICommand CommandDoubleClick
        {
            get
            {
                if (commandDoubleClick == null)
                {
                    commandDoubleClick = new RelayCommand(param => DoubleClickMethod(),
                    param => CanDoubleClick);
                }
                return commandDoubleClick;
            }
        }
    
        private void DoubleClickMethod()
        {
            MessageBox.Show("Double-Clicked the Border");
        }
    

    此代码放置在 ViewModel 中,该 ViewModel 用于包含透明边框的 Window 的 DataContext。您需要对 MVVM 设计模式、中继命令和绑定进行一些研究。

    不幸的是,在Border 或其他可以使用的面板元素上没有可直接访问的双击事件,因此需要更复杂的 InputBindings。

    【讨论】:

    • 感谢您的回答。但是如何添加一个事件,例如:如果双击边框,则应该出现一个带有文本“你双击”的消息框
    • 这就是命令绑定的用途。编辑了答案以包含 RelayCommand 和相关方法。
    • 非常感谢您的帮助!
    • 我可以在 MainWindows.xaml.cs 中使用 DoubleClickMethod 吗??
    • 我想您可以将CommandDoubleClick 命令和DoubleClickMethod 放在MainWindows.xaml.cs 中,然后在InitializeComponent 之后执行DataContext = this 并跳过ViewModel。你可以试一试,应该可以的。
    猜你喜欢
    • 1970-01-01
    • 2015-07-08
    • 2011-04-16
    • 1970-01-01
    • 2014-08-08
    • 2010-09-20
    • 2014-09-11
    • 1970-01-01
    • 2010-12-09
    相关资源
    最近更新 更多