【发布时间】:2019-03-25 02:12:09
【问题描述】:
我正在使用 VLC 插件编写 MediaPlayer,并希望使 vlc (WindowsFormsintegration) 可双击(全屏)。但是没有双击事件。所以我想到了一个覆盖 VLC (WFI) 的可点击透明面板。有人可以帮助我吗?由于缺乏透明度,我在 Windows 上使用 WPF 并将 Winforms 转换为 WPF。
【问题讨论】:
我正在使用 VLC 插件编写 MediaPlayer,并希望使 vlc (WindowsFormsintegration) 可双击(全屏)。但是没有双击事件。所以我想到了一个覆盖 VLC (WFI) 的可点击透明面板。有人可以帮助我吗?由于缺乏透明度,我在 Windows 上使用 WPF 并将 Winforms 转换为 WPF。
【问题讨论】:
在 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。
【讨论】:
CommandDoubleClick 命令和DoubleClickMethod 放在MainWindows.xaml.cs 中,然后在InitializeComponent 之后执行DataContext = this 并跳过ViewModel。你可以试一试,应该可以的。