本文介绍了WPF-将列表绑定到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个wpf应用程序并从我的USB网络摄像头捕获图像.我试过的是将所有捕获的图像存储在List
中,并在Listview
I'm creating a wpf application and capturing images from my usb webcam. What I have tried is store all captured images in a List
and show them in a Listview
public List<BitmapImage> listOfCapturedImages = new List<BitmapImage>();
private void addNewImageButton_Click(object sender, RoutedEventArgs e)
{
CameraWindow cw = new CameraWindow(this);
cw.newlyCapturedImage += (BitmapImage newImage) =>
{
listOfCapturedImages.Add(newImage);
newlyAddedImage.Source = newImage;
};
cw.Show();
}
XAML:
<ListView ItemsSource="{Binding listOfCapturedImages}" Height="345" Margin="577,10,10,0" VerticalAlignment="Top">
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="GridViewColumnHeader">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn x:Name="previewImagesColumn">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Button x:Name="firstImageOflistViewButton" Content="{Binding listOfCapturedImages}" Height="50">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter/>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
有人可以帮我吗,我想念的是什么?
Could someone help me please, what I'm missing?
推荐答案
非常感谢,我已经解决了您的帮助问题.我只想与可能与我有相同问题的其他人共享我的工作代码.这是工作代码:
Thanks a lot guys, I have fixed my problem with your helps. I want to share my working code just for someone else who may have the same problem with me. Here is the working code:
public ObservableCollection<BitmapImage> listOfCapturedImages { get; } =
new ObservableCollection<BitmapImage>();
private void addNewImageButton_Click(object sender, RoutedEventArgs e)
{
CameraWindow cw = new CameraWindow(this);
cw.newlyCapturedImage += (BitmapImage newImage) =>
{
listOfCapturedImages.Add(newImage);
newlyAddedImage.Source = newImage;
};
cw.Show();
}
我还添加了this.DataContext = this;
.
public Test(Window window)
{
InitializeComponent();
this.DataContext = this;
}
最后是 XAML:
<ListView ItemsSource="{Binding listOfCapturedImages}" Height="345" Margin="577,10,10,0" VerticalAlignment="Top">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这篇关于WPF-将列表绑定到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!