在当前选定的ListBox项下打开详细信息面板

时间:2016-07-14 22:46:42

标签: c# wpf xaml layout listbox

我有一个使用Listbox的以下布局的电影库:

image

当用户在库中选择电影时,我需要打开一个包含电影详细信息的框,如下所示:

image

我设法创建了一个差距和细节面板,但不知道如何将其移动到那里。

如果有人知道如何做到这一点,或者可以提出更优雅的解决方案,我将不胜感激。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我使用了DataGrid,因为它提供了RowDetailsTemplate

每行只包含一张图片。

有三个DataGrids

您可以设置所需的列数。

我还没有完成DataGrids的样式,所以你会看到额外的边框等。我会删除它们然后更新答案。

Output

您可以按原样使用以下代码:

的Xaml

<Window x:Class="WpfApplicationTest.WinNewGrid" ...>
    <Window.Resources>       
        <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
            <DataGridRowsPresenter x:Name="PART_RowsPresenter" IsItemsHost="True" Orientation="Horizontal"/>
        </ItemsPanelTemplate>

        <DataTemplate x:Key="DetailsKey">
          <StackPanel DataContext="{Binding SelectedMovie}">
            <TextBlock Height="50" Background="Red" Text="{Binding SourcePath}"/>
            <TextBlock Height="50" Background="Red" Text="{Binding ID}"/>
          </StackPanel>
         </DataTemplate>
    </Window.Resources>
    <Grid>

        <DataGrid x:Name="Dgrd" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Sets}" RowDetailsTemplate="{StaticResource DetailsKey}">

                                <DataGrid.Columns>
                                    <DataGridTemplateColumn>
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <DataGrid ItemsSource="{Binding MovieImageList}" SelectedItem="{Binding SelectedMovie, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" AutoGenerateColumns="False">                                                    
                                                    <DataGrid.Columns>
                                                        <DataGridTemplateColumn>
                                                            <DataGridTemplateColumn.CellTemplate>
                                                                <DataTemplate>
                                                                    <Image Source="{Binding SourcePath}" Width="50" Height="50"/>
                                                                </DataTemplate>
                                                            </DataGridTemplateColumn.CellTemplate>
                                                        </DataGridTemplateColumn>
                                                    </DataGrid.Columns>
                                                </DataGrid>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>


    </Grid>
</Window>

代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace WpfApplicationTest
{
    /// <summary>
    /// Interaction logic for WinNewGrid.xaml
    /// </summary>
    public partial class WinNewGrid : Window
    {
        public WinNewGrid()
        {
            InitializeComponent();

            List<MovieImage> images = new List<MovieImage>();
            foreach (string fileName in System.IO.Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures"))
            {
                if (fileName.EndsWith("jpg") || fileName.EndsWith("png") || fileName.EndsWith("gif"))
                    images.Add(new MovieImage() { SourcePath = new Uri(fileName, UriKind.Absolute), ID = Guid.NewGuid().ToString() });
            }

            MovieAndDetailsViewModel movieDetailsVm = new MovieAndDetailsViewModel(images);
            movieDetailsVm.Columns = 3;

            this.DataContext = this;
            Dgrd.ItemsSource = movieDetailsVm.ItemsSource;
        }
    }

    public class MovieAndDetailsViewModel
    {
        private MovieSetsViewModel _setViewModel;
        private List<MovieSetsViewModel> _list;
        public IEnumerable<MovieSetsViewModel> ItemsSource 
        { 
            get {
                _setViewModel = new MovieSetsViewModel();
                int pointer = 0;
                int cols = 6;
                List<MovieListViewModel>[] images = Data.Count() % cols > 0 ? new List<MovieListViewModel>[Data.Count() / cols + 1]
                    : new List<MovieListViewModel>[Data.Count() / cols];

                while (pointer < images.Count())
                {
                    MovieListViewModel mlistVm = new MovieListViewModel();
                    for (int i = pointer * cols; i < Data.Count() && i <= pointer * cols + cols - 1; ++i)
                    {
                        MovieImage img = Data.ToList()[i];
                        mlistVm.MovieImageList.Add(img);
                    }
                    _setViewModel.Sets.Add(mlistVm);
                    pointer += 1;
                }

                _list = new[] { _setViewModel }.ToList();
                return _list; 
            }
        }

        public IEnumerable<MovieImage> Data { get; set; }

        public int Columns { get; set; }

        public MovieAndDetailsViewModel(IEnumerable<MovieImage> movieImages)
        {
            Data = movieImages;

            Columns = 4;
        }
    }

    public class MovieSetsViewModel
    {
        public List<MovieListViewModel> Sets { get; set; }

        public MovieSetsViewModel()
        {
            Sets = new List<MovieListViewModel>();
        }
    }

    public class MovieListViewModel
    {
        public List<MovieImage> MovieImageList { get; set; }

        /* This gets updated while choosing a movie in DataGrid */
        public MovieImage SelectedMovie { get; set; }

        public MovieListViewModel()
        {
            MovieImageList = new List<MovieImage>();
            SelectedMovie = new MovieImage();
        }
    }

    public class MovieImage
    {
        public Uri SourcePath { get; set; }
        public string ID { get; set; }
    }
}