C# ListView加载图片,数据绑定和按钮

C# ListView加载图片,数据绑定和按钮。

在学习此功能时,百度发现,关于这块说的都不是特别完善。特此记录一下提醒自己。

界面代码

       <ListView Name="list1"   Height="200" Width="600"  SelectionChanged="List1_SelectionChanged">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="4"/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="列2" Width="96">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Image Source="{Binding image}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="列3" Width="50">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Width="30" Content="..." Click="Button_Click" Margin="-50,0,0,0" Background="Transparent"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

然后需要把图片列表绑定到前台,这需要新建一个类:

public class TestListView: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName.Trim()));
        protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Equals(storage, value))
            {
                return false;
            }
            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        private BitmapImage _image;
        public BitmapImage image
        {
            get { return _image; }
            set
            {
                _image = value;
                OnPropertyChanged("image");
            }
        }

        public TestListView() { }

        public TestListView(BitmapImage image)
        {
            this.image = image;
        }
    }

现在就开始绑定了:这块代码,我写在xaml文件对应的cs文件下。

public ObservableCollection<TestListView> testlist = new ObservableCollection<TestListView>();
        public UserControl2()
        {
            InitializeComponent();
            Listinit();
        }
        public void Listinit()
        {
            testlist.Add(new TestListView(GetImage("C:\\Users\\Public\\DOMA\\image\\age\\3.png")));
            testlist.Add(new TestListView(GetImage("C:\\Users\\Public\\DOMA\\image\\age\\3.png")));
            testlist.Add(new TestListView(GetImage("C:\\Users\\Public\\DOMA\\image\\age\\3.png")));
            testlist.Add(new TestListView(GetImage("C:\\Users\\Public\\DOMA\\image\\age\\3.png")));
            testlist.Add(new TestListView(GetImage("C:\\Users\\Public\\DOMA\\image\\age\\3.png")));
            list1.ItemsSource = testlist;
        }

        public static BitmapImage GetImage(string imagePath)
        {
            BitmapImage bitmap = new BitmapImage();
            //if (File.Exists(imagePath)) //永远返回false
            //{
            bitmap.BeginInit();
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            using (Stream ms = new MemoryStream(File.ReadAllBytes(imagePath)))
            {
                bitmap.StreamSource = ms;
                bitmap.EndInit();
                bitmap.Freeze();
            }
            //}
            return bitmap;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值