单击按钮将图像插入Windows 8应用程序

时间:2014-03-25 10:14:55

标签: c# windows-8.1

我正在创建一个Windows 8应用程序,并想知道最好的方法。我想要做的是在我的应用程序的侧面显示一个在另一个下面的图像。因此,在按钮上单击图像会出现在已存在的图像下。有没有人知道我会怎么做的任何例子或任何建议

1 个答案:

答案 0 :(得分:1)

使用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;
        }