我创建了一个窗口,用户可以在其中选择一个目录,然后该目录中的所有文件都显示在listBox中。每个文件都显示一个复选框和文件名。
现在我想显示一个工具提示,如果该文件是“知道”图像文件。到目前为止,我得到的是,如果选择路径,则会加载所有工具提示。这很好用。但是我遇到了问题,如果目录中有很多图像,应用程序会变得很慢。
我在视图中列表框的当前代码如下所示:
<ListBox Grid.Row="1" ItemsSource="{Binding DirectoryItems, UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=FilePath, Converter={StaticResource directoryItemConverter}}" VerticalAlignment="Center"
VerticalContentAlignment="Center" ToolTip="{Binding Converter={StaticResource directoryItemToolTipConverter}}"
IsChecked="{Binding Upload, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
DirectoryItems是一个类型为UploadFile的ObservableCollection。
Upload-File类是:
class UploadFile
{
public UploadFile(string filePath)
{
this.FilePath = filePath;
this.Upload = false;
}
public string FilePath { get; private set; }
public bool Upload { get; set; }
}
视图中使用的DirectoryItemToolTipConverter如下所示:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
UploadFile uploadFile = (UploadFile)value;
string extension = Path.GetExtension(uploadFile.FilePath);
if (extension != null && knownImageExtension.Contains(extension.ToLower()))
{
Bitmap bitmap = new Bitmap(System.Drawing.Image.FromFile(uploadFile.FilePath), previewImageSize, previewImageSize);
Image image = new Image
{
Source = ImageToImageSourceConverter.ConvertBitmap(bitmap),
Width = previewImageSize,
Height = previewImageSize
};
return image;
}
return null;
}
如何实现工具提示当前加载,鼠标第一次进入复选框?是否有“简单”的方法来实现这一目标?
答案 0 :(得分:0)
这应该做的工作
<CheckBox Content="{Binding Path=FilePath, Converter={StaticResource directoryItemConverter}}"
VerticalAlignment="Center" VerticalContentAlignment="Center"
IsChecked="{Binding Upload, UpdateSourceTrigger=PropertyChanged}">
<CheckBox.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard Storyboard.TargetProperty="ToolTip">
<ObjectAnimationUsingKeyFrames Duration="0:0:0">
<DiscreteObjectKeyFrame Value="{Binding Converter={StaticResource directoryItemToolTipConverter}}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</CheckBox.Triggers>
</CheckBox>