首页 > 其他分享 >Card(卡片)

Card(卡片)

时间:2022-08-23 22:46:16浏览次数:54  
标签:卡片 Title CardDtos Add CardDto new public Card

1.效果

效果图:

在这里插入图片描述

2.安装 MaterialDesignThemes

在这里插入图片描述

3.修改App.xaml

  • 引入命名空间
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
  • Application.Resources内添加代码
<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <materialDesign:BundledTheme
                    BaseTheme="Light"
                    PrimaryColor="DeepPurple"
                    SecondaryColor="Lime" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
  • 注意:
    • NuGet引入的工具包 MaterialDesignThemes GitHub → Wiki菜单中的 Super Quick Start (超级快速入门)

    • BaseTheme 设置主题颜色 Light(亮的);Dark(黑色)

4.单个Card

  • 粘贴图片后,点击图片,属性 → 生成操作 改成 资源
<Window
    x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp1"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <materialDesign:Card
            Width="420"
            Height="270"
            Margin="4,2,8,16">
            <Grid VerticalAlignment="Stretch">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Image
                    Grid.RowSpan="5"
                    VerticalAlignment="Stretch"
                    Source="./Contact.png"
                    Stretch="UniformToFill"
                    StretchDirection="Both" />

                <TextBlock
                    Grid.Column="1"
                    Margin="16,16,16,2"
                    Style="{StaticResource MaterialDesignHeadline5TextBlock}"
                    Text="Get In Touch" />

                <TextBlock
                    Grid.Row="1"
                    Grid.Column="1"
                    Margin="16,0,16,8"
                    VerticalAlignment="Center"
                    Foreground="{DynamicResource MaterialDesignBodyLight}"
                    Text="Say hello, make a feature request, or raise a bug through one of these channels:"
                    TextWrapping="Wrap" />

                <Border
                    Grid.Row="2"
                    Grid.Column="1"
                    Margin="0,8,0,0"
                    BorderBrush="{DynamicResource MaterialDesignDivider}"
                    BorderThickness="0,1,0,0">
                    <Grid Margin="8">
                        <Button
                            x:Name="GitHubButton"
                            HorizontalAlignment="Left"
                            Style="{StaticResource MaterialDesignFlatButton}">
                            <StackPanel Orientation="Horizontal">
                                <materialDesign:PackIcon Kind="Github" />
                                <TextBlock
                                    Margin="8,0,0,0"
                                    VerticalAlignment="Center"
                                    Text="GitHub" />
                            </StackPanel>
                        </Button>

                        <Button
                            x:Name="TwitterButton"
                            HorizontalAlignment="Right"
                            Style="{StaticResource MaterialDesignFlatButton}">
                            <StackPanel Orientation="Horizontal">
                                <materialDesign:PackIcon Kind="Twitter" />
                                <TextBlock
                                    Margin="8,0,0,0"
                                    VerticalAlignment="Center"
                                    Text="Twitter" />
                            </StackPanel>
                        </Button>
                    </Grid>
                </Border>

                <Border
                    Grid.Row="3"
                    Grid.Column="1"
                    BorderBrush="{DynamicResource MaterialDesignDivider}"
                    BorderThickness="0,1,0,0">
                    <Grid Margin="8">
                        <Button
                            x:Name="ChatButton"
                            HorizontalAlignment="Left"
                            Cursor="Hand"
                            Style="{StaticResource MaterialDesignFlatButton}">
                            <StackPanel Orientation="Horizontal">
                                <materialDesign:PackIcon Kind="Message" />
                                <TextBlock
                                    Margin="8,0,0,0"
                                    VerticalAlignment="Center"
                                    Text="Chat" />
                            </StackPanel>
                        </Button>

                        <Button
                            x:Name="EmailButton"
                            HorizontalAlignment="Right"
                            Style="{StaticResource MaterialDesignFlatButton}">
                            <StackPanel Orientation="Horizontal">
                                <materialDesign:PackIcon Kind="Email" />
                                <TextBlock
                                    Margin="8,0,0,0"
                                    VerticalAlignment="Center"
                                    Text="Email" />
                            </StackPanel>
                        </Button>
                    </Grid>
                </Border>
            </Grid>
        </materialDesign:Card>
    </Grid>
</Window>

5.数据模板

  • 我使用prism框架

  • 实体类

    public class CardDto : BindableBase
    {
        private string title;

        public string Title
        {
            get { return title; }
            set { title = value; RaisePropertyChanged(); }
        }

        private string content;

        public string Content
        {
            get { return content; }
            set { content = value; RaisePropertyChanged(); }
        }
    }
  • MainViewModel
    public class MainViewModel : BindableBase
    {
        public MainViewModel()
        {
            CardDtos = new ObservableCollection<CardDto>();
            Add();
        }

        private ObservableCollection<CardDto> cardDtos;
        public ObservableCollection<CardDto> CardDtos
        {
            get { return cardDtos; }
            set
            {
                cardDtos = value;
                RaisePropertyChanged();
            }
        }

        void Add()
        {
            CardDtos.Add(new CardDto() { Title = "111", Content = "测试1下吧" });
            CardDtos.Add(new CardDto() { Title = "222", Content = "测试2下吧" });
            CardDtos.Add(new CardDto() { Title = "333", Content = "测试3下吧" });
        }
    }
  • MainView.xaml
<Window
    x:Class="WpfApp1.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp1"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="http://prismlibrary.com/"
    Title="MainWindow"
    Width="1200"
    Height="800"
    prism:ViewModelLocator.AutoWireViewModel="True"
    mc:Ignorable="d">
    <Grid>
        <StackPanel Grid.Column="1">
            <ScrollViewer Width="500" Height="600">
                <ListBox x:Name="Lbx" ItemsSource="{Binding CardDtos}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <materialDesign:Card
                                Width="300"
                                Height="170"
                                Margin="4,2,8,16">
                                <Grid VerticalAlignment="Stretch">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <TextBlock
                                        Margin="16,16,16,2"
                                        Style="{StaticResource MaterialDesignHeadline5TextBlock}"
                                        Text="{Binding Title}" />
                                    <TextBlock
                                        Grid.Row="1"
                                        Margin="16,0,16,8"
                                        VerticalAlignment="Center"
                                        Foreground="{DynamicResource MaterialDesignBodyLight}"
                                        Text="{Binding Content}"
                                        TextWrapping="Wrap" />
                                </Grid>
                            </materialDesign:Card>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </ScrollViewer>
        </StackPanel>
    </Grid>
</Window>

标签:卡片,Title,CardDtos,Add,CardDto,new,public,Card
From: https://www.cnblogs.com/DotNeter-Hpf/p/16618106.html

相关文章

  • 直播电商平台开发,uni-app实现卡片切换效果
    直播电商平台开发,uni-app实现卡片切换效果第一步:卡片的切换 <viewclass='bgDemo'><viewv-for="item,indexinobjectMultiArray":key="index"@click='bindMultiPic......
  • SAS 用cards/datalines读入原始数据举例
    SAS用cards/datalines读入原始数据:input作用:1)当数据没有这个变量时生成新变量2)读取cards或外部数据。语法:inputinformat.  在input设定的输入格式并不存储在创......
  • 前端页面串联卡片的一个思路
    串联卡片的一个思路需求描述做一行固定宽度但是数量不固定的的卡片,卡片前后通过箭头链接,箭头要处在两个卡片中间位置。问题分析首先需要一个卡片的基础组件,然后遍历数......
  • 卡片布局以及鼠标悬浮展示全部
    界面要求不论视口大小每行显示6个元素,文字超出长显示省略号(暂定显示一行)当鼠标悬浮的时候,展示所有文字,卡片向下延展直到能够完全显示文字先看效果图分析第一......
  • uniapp 卡片式轮播图
    html结构:uniapp自带的swiper组件可以弄成寻常的轮播图 <swiperclass="image-container"previous-margin="45rpx" next-margin="45rpx"circularautopl......