如何定义 WPF ListView 的每个项目,该视图看起来类似于 Windows 7 中的超大图标视图?

发布于 2024-10-21 16:56:24 字数 598 浏览 2 评论 0原文

基本上我正在尝试做类似 this http://www .crunchgear.com/wp-content/uploads/2010/07/popbox.jpg

仅以固定尺寸显示图像,图像下方没有文字。图像之间的水平和垂直距离也应该是固定的。

另外,如果有人可以添加鼠标悬停发光效果来模拟相同的效果,那就太酷了。

我现在对 xaml 更熟悉了,但我对为 ListView 控件定义每个项目感到困惑。我之前只使用过 WPF ListViewGridViewColumns,所以这是新的。

另外,我尝试绑定具有图像的 ListView 的对象是这样的:

class Film
{
    Image Image {get;set;}
}

但是如果我应该将它们存储在 Image 之外的不同类型中,那也很酷。

Basically I am trying to do something like this http://www.crunchgear.com/wp-content/uploads/2010/07/popbox.jpg.

Just showing images in a fixed dimension with no text below the images. It also should have a fixed distance between the images horizontally and vertically.

Also if someone could add a mouseover glow effect to simulate the same thing, that would be cool.

I am more familiar with xaml now but I just got confused about defining each item for a ListView control. I only used WPF ListView with GridViewColumns before so this is new.

Also the object I am trying to bind the ListView that has the images is something like this:

class Film
{
    Image Image {get;set;}
}

But if I should store them in a different type other than Image, that's cool too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜味超标? 2024-10-28 16:56:24

这是一个快速而肮脏的示例,它提供了与您想要的效果类似的效果:

XAML

<Window x:Class="WpfListViewDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfListViewDemo"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView ItemsSource="{Binding Films}"
                  Background="Black">

            <!-- Panel that will contains the items -->
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True"
                               ItemWidth="150" ItemHeight="220"
                               Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType=ScrollContentPresenter}}"/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

            <!-- Template for each item -->
            <ListView.ItemTemplate>
                <DataTemplate DataType="{x:Type my:Film}">
                    <Grid>
                        <!-- Halo that will be shown when the mouse is over the image -->
                        <Ellipse Name="mouseOverHalo" Visibility="Hidden"
                                 Width="180" Height="250"
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Center"
                                 ClipToBounds="False">
                            <Ellipse.Fill>
                                <RadialGradientBrush Center="0.5, 0.5">
                                    <GradientStop Offset="0.0" Color="Blue" />
                                    <GradientStop Offset="0.8" Color="Blue" />
                                    <GradientStop Offset="1.0" Color="Black" />
                                </RadialGradientBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                        <Image Name="img"
                               Source="{Binding ImagePath}"
                               ToolTip="{Binding Title}"
                               Margin="10" />
                    </Grid>
                    <DataTemplate.Triggers>
                        <!-- Trigger to display the halo when the mouse is over the image -->
                        <Trigger SourceName="img" Property="IsMouseOver" Value="True">
                            <Setter TargetName="mouseOverHalo"
                                    Property="Visibility"
                                    Value="Visible">
                            </Setter>
                        </Trigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

Film class

public class Film
{
    public string Title { get; set; }
    public string ImagePath { get; set; }
}

Code-behind

(您也可以使用 ViewModel类作为 DataContext,为了简单起见,我在这里使用了代码隐藏)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Films = new ObservableCollection<Film>(LoadFilms());
        this.DataContext = this;
    }

    private static IEnumerable<Film> LoadFilms()
    {
        string imagesDirectory = @"D:\Docs\DVD\covers";
        return
            from file in Directory.EnumerateFiles(imagesDirectory)
            select new Film
                {
                    Title = Path.GetFileNameWithoutExtension(file),
                    ImagePath = file
                };

    }

    public ObservableCollection<Film> Films { get; private set; }
}

光环现在很丑陋,但是嘿,我不是设计师;)

Here's a quick and dirty example that gives an effect similar to what you want:

XAML

<Window x:Class="WpfListViewDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfListViewDemo"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView ItemsSource="{Binding Films}"
                  Background="Black">

            <!-- Panel that will contains the items -->
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True"
                               ItemWidth="150" ItemHeight="220"
                               Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType=ScrollContentPresenter}}"/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

            <!-- Template for each item -->
            <ListView.ItemTemplate>
                <DataTemplate DataType="{x:Type my:Film}">
                    <Grid>
                        <!-- Halo that will be shown when the mouse is over the image -->
                        <Ellipse Name="mouseOverHalo" Visibility="Hidden"
                                 Width="180" Height="250"
                                 VerticalAlignment="Center"
                                 HorizontalAlignment="Center"
                                 ClipToBounds="False">
                            <Ellipse.Fill>
                                <RadialGradientBrush Center="0.5, 0.5">
                                    <GradientStop Offset="0.0" Color="Blue" />
                                    <GradientStop Offset="0.8" Color="Blue" />
                                    <GradientStop Offset="1.0" Color="Black" />
                                </RadialGradientBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                        <Image Name="img"
                               Source="{Binding ImagePath}"
                               ToolTip="{Binding Title}"
                               Margin="10" />
                    </Grid>
                    <DataTemplate.Triggers>
                        <!-- Trigger to display the halo when the mouse is over the image -->
                        <Trigger SourceName="img" Property="IsMouseOver" Value="True">
                            <Setter TargetName="mouseOverHalo"
                                    Property="Visibility"
                                    Value="Visible">
                            </Setter>
                        </Trigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

Film class

public class Film
{
    public string Title { get; set; }
    public string ImagePath { get; set; }
}

Code-behind

(You could also use a ViewModel class as the DataContext, I used code-behind here for the sake of simplicity)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Films = new ObservableCollection<Film>(LoadFilms());
        this.DataContext = this;
    }

    private static IEnumerable<Film> LoadFilms()
    {
        string imagesDirectory = @"D:\Docs\DVD\covers";
        return
            from file in Directory.EnumerateFiles(imagesDirectory)
            select new Film
                {
                    Title = Path.GetFileNameWithoutExtension(file),
                    ImagePath = file
                };

    }

    public ObservableCollection<Film> Films { get; private set; }
}

The halo is quite ugly right now, but hey, I'm not a designer ;)

决绝 2024-10-28 16:56:24

我会使用 ItemsControl

作为 ItemsPanel,我将使用 WrapPanel。作为 ItemTemplate,您可以提供绑定到胶片图像的 DataTemplate,最好将图像提供为 ImageSource,然后您可以立即将其用作 Image 元素的源。

为您的图像定义一个样式,在其中为每个 FrameworkElement 都有的 IsMouseOver = true 定义一个触发器,无论您想要什么当鼠标悬停在图像上时的视觉效果。

I would use an ItemsControl.

As ItemsPanel I would use a WrapPanel. As ItemTemplate you can provide a DataTemplate binding to the Image of your Film, better provide the Image as ImageSource, then you can use it straight away as source for an Image element.

Define a Style for your Image in which you define a Trigger for the IsMouseOver = true that every FrameworkElement has, whatever eye-candy you want when the mouse is over the image.

半山落雨半山空 2024-10-28 16:56:24

啊,正忙着模拟一个例子:) 无论如何我都会发布它:我添加了一个投影,但你可以轻松地将其更改为发光。,C# 部分只是从旁边的文件夹填充文件名列表。EXE文件。

XAML:

<Window x:Class="pic_viewer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:pic_viewer"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <DataTemplate x:Key="item_template" DataType="Item">
        <Image Source="{Binding}"  Width="64" Height="64"/>
        <DataTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BitmapEffect">
                <Setter.Value>
                    <DropShadowBitmapEffect ShadowDepth="3" Color="Black"/>
                </Setter.Value>
            </Setter>
            </Trigger>
        </DataTemplate.Triggers>
    </DataTemplate>
    <ObjectDataProvider x:Key="pic_list" ObjectType="{x:Type local:Pic}" MethodName="get_pics"/>
</Window.Resources>

<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled" x:Name="item_listbox"
             ItemsSource="{Binding Source={StaticResource pic_list}}" ItemTemplate="{StaticResource item_template}">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>
</Window>

C#:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;

namespace pic_viewer
{
    public partial class MainWindow : Window
   {
        public MainWindow()
        {
           InitializeComponent();
        }
    }

    public class Pic
    {
        public List<string> get_pics()
        {            
            List<string> p = new List<string>();
            p.Add(@"pack://siteoforigin:,,,/Images/black.png");
            p.Add(@"pack://siteoforigin:,,,/Images/blu.png");
            p.Add(@"pack://siteoforigin:,,,/Images/empty.png");
            p.Add(@"pack://siteoforigin:,,,/Images/red.png");
            return p;
        }
    }
}

Ah, was busy mocking up an example :) I'll post it anyway: I added a drop shadow but you can easily change it to a glow., and the C# part just populates a list of file names from a folder next to the .exe.

XAML:

<Window x:Class="pic_viewer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:pic_viewer"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <DataTemplate x:Key="item_template" DataType="Item">
        <Image Source="{Binding}"  Width="64" Height="64"/>
        <DataTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BitmapEffect">
                <Setter.Value>
                    <DropShadowBitmapEffect ShadowDepth="3" Color="Black"/>
                </Setter.Value>
            </Setter>
            </Trigger>
        </DataTemplate.Triggers>
    </DataTemplate>
    <ObjectDataProvider x:Key="pic_list" ObjectType="{x:Type local:Pic}" MethodName="get_pics"/>
</Window.Resources>

<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled" x:Name="item_listbox"
             ItemsSource="{Binding Source={StaticResource pic_list}}" ItemTemplate="{StaticResource item_template}">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>
</Window>

C#:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;

namespace pic_viewer
{
    public partial class MainWindow : Window
   {
        public MainWindow()
        {
           InitializeComponent();
        }
    }

    public class Pic
    {
        public List<string> get_pics()
        {            
            List<string> p = new List<string>();
            p.Add(@"pack://siteoforigin:,,,/Images/black.png");
            p.Add(@"pack://siteoforigin:,,,/Images/blu.png");
            p.Add(@"pack://siteoforigin:,,,/Images/empty.png");
            p.Add(@"pack://siteoforigin:,,,/Images/red.png");
            return p;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文