将ListBox / ListView自定义为“FlipView”(图片)

时间:2016-01-26 11:42:39

标签: c# wpf wpf-controls

我想自定义我的列表框或列表视图,使其行为与下图中的控件相似:

enter image description here

它类似于FlipView,但我以前从未使用过FlipView,只看过一些照片。

2 个答案:

答案 0 :(得分:1)

我找到了一个很好的解决方案。它可能会帮助某人。我已经改变了一点,它对我来说很完美。

http://www.codeproject.com/Articles/741026/WPF-FlipView

答案 1 :(得分:0)

尝试这样的事情

<UserControl x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         >
<UserControl.Resources>
    <DataTemplate x:Key="Test">

        <Grid >
            <Border Background="Red"
                    Loaded="RedBorder_OnLoaded" >
                <!--content for this card goes here-->
                <TextBlock Text="{Binding}"></TextBlock>
            </Border>
            <Border Background="Green"
                    Loaded="GreenBorder_OnLoaded"
                    Visibility="Collapsed" >
                <!--content for this card goes here-->
                <TextBlock Text="{Binding}"></TextBlock>
            </Border>
        </Grid>

    </DataTemplate>
</UserControl.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ListBox Name="myListbox"
             Margin="50"
             HorizontalContentAlignment="Stretch"
             VerticalContentAlignment="Stretch"
             ItemTemplate="{StaticResource Test}" />
    <StackPanel Grid.Row="1"
                HorizontalAlignment="Center"
                Orientation="Horizontal">
        <Button Width="20"
                Height="20"
                Background="Black"
                Click="FirstButton_OnClick" />
        <Button Width="20"
                Height="20"
                Background="Black"
                Click="SecondButton_OnClick" />
    </StackPanel>
</Grid>

</UserControl>

背后的代码

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

namespace WpfApplication1
{
public partial class UserControl1 : UserControl
{
    private readonly List<Border> redBorders = new List<Border>();
    private readonly List<Border> greenBorders = new List<Border>();

    public UserControl1()
    {
        InitializeComponent();
        myListbox.ItemsSource = new List<string>() { "Batman", "Superman",     "All others" };
    }

    private void RedBorder_OnLoaded(object sender, RoutedEventArgs e)
    {
        redBorders.Add(sender as Border);
    }

    private void GreenBorder_OnLoaded(object sender, RoutedEventArgs e)
    {
        greenBorders.Add(sender as Border);
    }

    private void FirstButton_OnClick(object sender, RoutedEventArgs e)
    {
        redBorders.ForEach(p => p.Visibility = Visibility.Visible);
        greenBorders.ForEach(p => p.Visibility = Visibility.Collapsed);
    }

    private void SecondButton_OnClick(object sender, RoutedEventArgs e)
    {
        redBorders.ForEach(p => p.Visibility = Visibility.Collapsed);
        greenBorders.ForEach(p => p.Visibility = Visibility.Visible);
    }
}
}

用法

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wpfApplication1="clr-namespace:WpfApplication1">
<wpfApplication1:UserControl1 />

这很简单,但我想你可以从这里改进它。