打开文件时WPF列表选择问题
有一个WPF应用程序,它有一个悬停弹出窗口。弹出窗口包含可打开的不同文件列表(如pdf、excel等) 您可以通过双击导航和选择文件,并按预期打开该文件 但是如果我现在导航到另一个文件,我可以看到悬停选择现在不起作用 如果现在选择其他文件,则会再次打开原始文件 我正在使用Process.Start并将文件的完整路径传递给该方法 这个应用程序是一个相当大的应用程序,所以下面是我编写的一个测试应用程序的一些摘录,以进一步研究这个问题 主窗口的XAML打开文件时WPF列表选择问题,wpf,popup,process.start,Wpf,Popup,Process.start,有一个WPF应用程序,它有一个悬停弹出窗口。弹出窗口包含可打开的不同文件列表(如pdf、excel等) 您可以通过双击导航和选择文件,并按预期打开该文件 但是如果我现在导航到另一个文件,我可以看到悬停选择现在不起作用 如果现在选择其他文件,则会再次打开原始文件 我正在使用Process.Start并将文件的完整路径传递给该方法 这个应用程序是一个相当大的应用程序,所以下面是我编写的一个测试应用程序的一些摘录,以进一步研究这个问题 主窗口的XAML <Window x:Class="Test
<Window x:Class="TestPopupIssue.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas Margin="5" Background="Red" Width="200" Height="150" >
<Rectangle Name="rectangle1"
Canvas.Top="60" Canvas.Left="50"
Height="85" Width="60"
Fill="Black" MouseEnter="rectangle1_MouseEnter" MouseLeave="rectangle1_MouseLeave" />
<Popup x:Name="PopupWindow" PlacementTarget="{Binding ElementName=rectangle1}" Placement="Top" MouseEnter="rectangle1_MouseEnter" MouseLeave="rectangle1_MouseLeave">
<ListBox MinHeight="50" ItemsSource="{Binding Files}" MouseDoubleClick="FileList_MouseDoubleClick"`enter code here` x:Name="FileList" />
</Popup>
</Canvas>
</Window>
还有一个FileList类,它只有一个文件路径的通用字符串列表,名为
档案
谢谢当您使用Process打开文件时,我已经测试了您的示例应用程序。启动打开文件的应用程序会偷走您的焦点。 不知何故,当窗口失去焦点时,弹出窗口中的列表框无法更改其SelectedItem 不幸的是,我没有将焦点放回窗口,这个.SetFocus()对我不起作用 无论如何,另一个可能的解决方案是在打开文件时关闭弹出窗口
private void FileList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (FileList.SelectedItem != null)
{
string item = FileList.SelectedItem as string;
if (item != null)
{
System.Diagnostics.Process.Start(item);
PopupWindow.IsOpen = false;
}
}
}
这样列表框可以再次更新selectedItem
希望这有帮助 为什么你在两个控件上发射相同的MouseEnter和MouseLeave?我认为你需要简化它。这只是为了简化这个示例-原始的有淡入淡出等。在这里,当你离开矩形时,当你在没有其他鼠标的情况下悬停在其上时,弹出窗口关闭。谢谢,我们会看一看
private void FileList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (FileList.SelectedItem != null)
{
string item = FileList.SelectedItem as string;
if (item != null)
{
System.Diagnostics.Process.Start(item);
PopupWindow.IsOpen = false;
}
}
}