【发布时间】:2014-10-27 17:10:25
【问题描述】:
我制作了一个应用程序,可以从视频设备流式传输视频,并添加了截取视频的功能,当截取视频时,它会显示在 ItemsControl 中,并显示其余截取内容。
我的问题是如何使 ItemsControl 中显示的每个项目“图像”都可点击以便我可以预览它?
我将图像详细信息存储在一个列表中:
List<ImageDetails> images = new List<ImageDetails>();
这是 ItemsControl 的 Xaml 代码:
编辑:我将所有内容都包装在数据模板内的按钮模板中,并在按钮上调用了 mousebuttonclickup 事件,但没有任何反应。 我放错按钮模板了吗?
<ItemsControl Name="ImageList" ItemsSource="{Binding ImageList}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Width="auto" Height="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MouseLeftButtonUp="Button_MouseLeftButtonUp">
<Button.Template>
<ControlTemplate>
<Border x:Name="imagecanv" BorderThickness="1" BorderBrush="#FFD0D1D7" Padding="5" Margin="5,5,0,0" MouseLeftButtonUp="imagecanv_MouseLeftButtonUp">
<StackPanel Orientation="Horizontal">
<!--image and dimensions-->
<Grid x:Name="picgrid" Width="88" Height="55">
<Image x:Name="imgcontainer" Source="{Binding Path}" MouseLeftButtonUp="imgcontainer_MouseLeftButtonUp"/>
<TextBlock Background="#B2000000" Foreground="White" Height="16" TextAlignment="Center" VerticalAlignment="Bottom">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}x{1}">
<Binding Path="Height"/>
<Binding Path="Width"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
<!--name, type and size-->
<StackPanel Orientation="Vertical" Margin="5,0,0,0" VerticalAlignment="Center">
<TextBlock Name="ImageName" Margin="1" Foreground="#FF787878" Text="{Binding FileName}"/>
<TextBlock Name="ImageType" Margin="1" Foreground="#FF787878">
<TextBlock.Text>
<MultiBinding StringFormat="Type: {0}">
<Binding Path="Extension"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Name="ImageSize" Margin="1" Foreground="#FF787878">
<TextBlock.Text>
<MultiBinding StringFormat="Size: {0} Bytes">
<Binding Path="Size"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</StackPanel>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate >
<StackPanel x:Name="stackeditems" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer Name="Scroller" Padding="{TemplateBinding Padding}" HorizontalScrollBarVisibility="Visible" >
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
点击事件:
private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Clickable!");
}
这是查看捕获的代码:
private void capture_Click(object sender, RoutedEventArgs e)
{
// Create a Bitmap of the same dimension of panelVideoPreview (Width x Height)
using (Bitmap bitmap = new Bitmap(viewpanel.Width, viewpanel.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
// Get the paramters to call g.CopyFromScreen and get the image
System.Drawing.Rectangle rectanglePanelVideoPreview = viewpanel.Bounds;
System.Drawing.Point sourcePoints = viewpanel.PointToScreen(new System.Drawing.Point(viewpanel.ClientRectangle.X, viewpanel.ClientRectangle.Y));
g.CopyFromScreen(sourcePoints, System.Drawing.Point.Empty, rectanglePanelVideoPreview.Size);
}
string strGrabFileName = String.Format(@"d:\app result\\Snapshot_{0:yyyyMMdd_hhmmss}.jpg", DateTime.Now);
try
{
bitmap.Save(strGrabFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
imageview(strGrabFileName);
}
catch (Exception)
{
}
}
}
public void imageview (string file)
{
ImageDetails id = new ImageDetails()
{
Path = file,
FileName = System.IO.Path.GetFileName(file),
Extension = System.IO.Path.GetExtension(file)
};
BitmapImage img = new BitmapImage();
img.BeginInit();
img.CacheOption = BitmapCacheOption.OnLoad;
img.UriSource = new Uri(file, UriKind.Absolute);
img.EndInit();
id.Width = img.PixelWidth;
id.Height = img.PixelHeight;
// I couldn't find file size in BitmapImage
FileInfo fi = new FileInfo(file);
id.Size = fi.Length;
images.Add(id);
//images.Insert(i, id);
ImageList.ItemsSource = null;
ImageList.ItemsSource = images;
}
【问题讨论】:
标签: c# wpf itemscontrol