插入图像到Windows 8应用程序上的按钮点击

本文关键字:程序上 按钮 应用程序 应用 图像 Windows 插入 | 更新日期: 2023-09-27 17:53:13

我正在创建一个Windows 8应用程序,想知道最好的方法去做一些事情。我想做的是让图像一个在另一个下面显示在app的下面,在按钮点击时,一个图像出现在已经存在的图像下面。有谁知道我该怎么做的例子或建议吗?

插入图像到Windows 8应用程序上的按钮点击

使用Listbox,

<Grid  >
    <ListView ItemsSource="{Binding PhotoList}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding PhotoName}"></TextBlock>
                    <Image Source="{Binding Photo}" Width="100" Height="100"></Image>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>
你们班

:

 public class PhotoItem
    {
        public string PhotoName { get; set; }
        public BitmapImage Photo { get; set; }
        public static List<PhotoItem> GetPhotos()
        {
            return new List<PhotoItem>()
            {
                new PhotoItem(){PhotoName="Image1",Photo = new BitmapImage(new Uri("/Images/Image1.jpg", UriKind.Relative))},
                new PhotoItem(){PhotoName="Image2",Photo = new BitmapImage(new Uri("/Images/Image2.jpg", UriKind.Relative))},
            };
        }
    }

ViewModel.cs

public class PhotoItemViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<PhotoItem> photoList;
        public ObservableCollection<PhotoItem> PhotoList
        {
            get
            {
                return photoList;
            }
            set
            {
                photoList = value;
                NotifyPropertyChanged();
            }
        }
        public void LoadData()
        {
            PhotoList = new ObservableCollection<PhotoItem>(PhotoItem.GetPhotos());
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
在mainPage.cs

 PhotoItemViewModel viewModel = new PhotoItemViewModel();
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel.LoadData();
            DataContext = viewModel;
        }