[英]WPF- Bind list to listview
I'm creating a wpf application and capturing images from my usb webcam. 我正在创建一个wpf应用程序并从我的usb网络摄像头捕获图像。 What I have tried is store all captured images in a
List
and show them in a Listview
我所尝试的是将所有捕获的图像存储在
List
并在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: 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();
}
I also have added this.DataContext = this;
我也添加了
this.DataContext = this;
. 。
public Test(Window window)
{
InitializeComponent();
this.DataContext = this;
}
And finally XAML: 最后是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>
Couple of problems 几个问题
Use ObservableCollection
instead of List
. 使用
ObservableCollection
而不是List
。 List
do not notify View to update itself when an item is added to it. List
不会通知View在添加项目时更新自身。 ObservableCollection
does that. ObservableCollection
就是ObservableCollection
。
listOfCapturedImages
is a Field. listOfCapturedImages
是一个Field。 Even if you set is to public
, it cannot be used to bind in WPF. 即使你设置为
public
,它也不能用于在WPF中绑定。 Because Field are invisible to XAML
, whereas Properties are visible. 因为字段对
XAML
不可见,而属性是可见的。 So do this 这样做
public ObservableCollection<BitmapImage> listOfCapturedImages { get; } = new ObservableCollection<BitmapImage>();
You are binding to field not a property. 您绑定到字段而不是属性。
Change: 更改:
public List<BitmapImage> listOfCapturedImages = new List<BitmapImage>();
to 至
public ObservableCollection<BitmapImage> ListOfCapturedImages {get;} = new ObservableCollection<BitmapImage>();
ObservableCollection
is the collection that will notify of changing its contents and the binding will update after you add items to it. ObservableCollection
是将通知更改其内容的集合,并且在向其添加项目后绑定将更新。 List
will not do that. List
不会这样做。
Also include some Image
displaying item: 还包括一些
Image
显示项:
<ListView ItemsSource="{Binding ListOfCapturedImages}" >
<ListView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
You should have Image
control in your DataTemplate
. 您应该在
DataTemplate
拥有Image
控件。 This is what i am using: 这就是我正在使用的:
<ListView ItemsSource="{Binding listOfCapturedImages}" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.