WPF- 将列表绑定到列表视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43604966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:04:25  来源:igfitidea点击:

WPF- Bind list to listview

c#wpflistviewbinding

提问by

I'm creating a wpf application and capturing images from my usb webcam. What I have tried is store all captured images in a Listand show them in a Listview

我正在创建一个 wpf 应用程序并从我的 USB 网络摄像头捕获图像。我尝试过的是将所有捕获的图像存储在 a 中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?

有人可以帮我吗,我错过了什么?

回答by

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>

回答by Nikhil Agrawal

Couple of problems

几个问题

  1. Use ObservableCollectioninstead of List. Listdo not notify View to update itself when an item is added to it. ObservableCollectiondoes that.

  2. listOfCapturedImagesis a Field. Even if you set is to public, it cannot be used to bind in WPF. Because Field are invisible to XAML, whereas Properties are visible. So do this

    public ObservableCollection<BitmapImage> listOfCapturedImages { get; } = 
       new ObservableCollection<BitmapImage>();
    
  1. 使用ObservableCollection代替ListList当一个项目被添加到它时,不要通知 View 更新自身。ObservableCollection这样做。

  2. listOfCapturedImages是一个字段。即使设置为public,也不能用于WPF中的绑定。因为 Field 对 不可见XAML,而 Properties 是可见的。所以这样做

    public ObservableCollection<BitmapImage> listOfCapturedImages { get; } = 
       new ObservableCollection<BitmapImage>();
    

回答by Rafal

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>();

ObservableCollectionis the collection that will notify of changing its contents and the binding will update after you add items to it. Listwill not do that.

ObservableCollection是将通知更改其内容的集合,并且绑定将在您向其添加项目后更新。List不会那样做。

Also include some Imagedisplaying item:

还包括一些Image显示项目:

<ListView ItemsSource="{Binding ListOfCapturedImages}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

回答by swdev95

You should have Imagecontrol in your DataTemplate. This is what i am using:

你应该Image控制你的DataTemplate. 这就是我正在使用的:

<ListView ItemsSource="{Binding listOfCapturedImages}" >
     <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="1"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>