动态地将对象放入网格WPF c#

时间:2018-07-15 16:03:21

标签: c# wpf

我想制作一个从MySQL数据库获取名称和姓氏列表的应用程序,并显示所有用户,包括其姓名,姓氏和年龄,如下所示:

card   cards that should be displayed

现在每张卡都有相同的名称,姓氏,年龄和图片。我希望每张卡都可以使用数据库中的列表或其他东西自动创建(尚不确定那部分内容:P) 因此,就像卡片1会放一张Hatty Hattington的照片,名字是yoris fresh,年龄为17岁,卡片2则应该放一张一些花花公子的照片,上面有他的全名和年龄。 (我叫卡片标签) 因此基本上是使用MySQL的数据创建这些标签,然后将它们自动放入网格中。我正在使用WPF,对c#还是很陌生,但是我花了最后两天的时间研究如何做,却找不到方法。我曾经在PHP中这样做过(如果这可以使任何人对我要创建的内容有更好的了解):

     <?php function tag($name, $work, $quote, $foodnr, $image) {?>
<button class="button" type="button" name="button">
    <div class="TagContainer">
        <div class="ProfilePic">
            <img src="<?php print($image)?>.jpg" alt="" class="personalimg">
        </div>
        <div class="BottomPart">
            <p id="Name"><?php print($name);?></p>
            <p id="Work"><?php print($work);?></p>
            <p id="Quote">"<?php print($quote);?>"</p>
            <div class="FoodContainer">
                <p id="Food">Favorite food:</p>
                <div class="FavFoodCont">
                    <img class="favfood" src="foods/food(<?php print($foodnr);?>).png" alt="">
                </div>
            </div>
        </div>
    </div>
</button>
<style media="screen">
    @import url('https://fonts.googleapis.com/css?family=Montserrat|Quicksand');
    .TagContainer {
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
        display: inline-block;
        width: 247px;
        background-color: rgb(255, 255, 255);
    }
    .ProfilePic img {
        width: 100%;
        height: 21.7%;
    }
    .BottomPart #Name {
        display: block;
        font-size: 30px;
        font-family: Quicksand;
        text-align: center;
    }
    .BottomPart #Work {
        font-family: Montserrat;
        font-size: 30px;
        color: rgb(182, 182, 182);
        text-align: center;
    }
    .BottomPart #quote {
        font-family: Montserrat;
        font-size: 30px;
        color: rgb(115, 115, 115);
        text-align: center;
    }
    .FoodContainer #Food {
        float: left;
        width: 40%;
        font-family: quicksand;
        font-size: 30px;
    }
    .FavFoodCont img {
        width: 50%;
        float: right;
    }
    p #quote, #work, {
        word-wrap: break-word;
    }
    #Quote {
        font-size: 20px;
        font-family: Montserrat;
        font-color: rgb(164, 164, 164);
    }
    .TagContainer {
        vertical-align: top;
        margin-left: auto;
        margin-left: auto;
        margin-right: auto;
    }
</style>
<?php } ?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>test</title>
</head>
<body>
    <div class="allcont">
        <?php
        tag("not Joris Vaišvila", "CEO of apple", "If i wasn't narcistic - I would be perfect", 5, "Joris");
        tag("Joris Vaišvila", "nowhersssssssssssssssssssssssssse", "Im dumb", 5, "Joris");
        tag("Joris Vaišvila", "nowhere", "Im dumb", 15, "Joris");
        tag("Joris Vaišvila", "nowhere", "Im dumb", 50, "Joris");
        tag("Joris Vaišvila", "nowhere", "Im dumb", 9, "Joris");
        tag("Joris Vaišvila", "nowhere", "Im dumb", 5, "Joris");
        tag("Joris Vaišvila", "nowhere", "Im dumb", 5, "Joris");
        ?>
    </div>
</body>
<style media="screen">
    .allcont {
        display: inline-block;
        width: 60%;
        margin-left: 20%;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
    }
        .allcont p {
            word-wrap: break-word;
        }
    .button {
        outline: none;
        margin-top: 20px;
        border: none;
        background-color: white;
    }
        .button:focus {
            padding-top: 5px;
            padding-bottom: 5px;
            margin-top: 10px;
            background-color: rgb(153, 153, 153);
        }
</style>
</html>

这是我在C#中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public Person obje { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            obje = new Person();
            obje.Nameas = "Yoris";
            obje.LastName = "Fresh";
            obje.age = "17";
            this.DataContext = this;
        }
        public class Person
        {
            public string Nameas { get; set; }
            public string LastName { get; set; }
            public string age { get; set; }
        }
    }
}

和xaml:

    <Window x:Class="WpfApp3.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <DataTemplate x:Key="persontemplate">
            <Grid Margin="1,1,1,1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"></ColumnDefinition>
                    <ColumnDefinition Width="2*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Source="C:\Users\yoris\Downloads\hat.png" HorizontalAlignment="Left"/>
                <Grid Grid.Column="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Label FontSize="21" Grid.Row="0" Content="{Binding Nameas}"/>
                    <Label FontSize="21" Grid.Row="1" Content="{Binding LastName}"/>
                    <Label FontSize="21" Grid.Row="2" Content="{Binding age}"/>
                </Grid>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid Name="maingrid"> 
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ContentControl Content="{Binding obje}" ContentTemplate="{StaticResource persontemplate}" Grid.Column="0" Grid.Row="0" />
        <ContentControl Content="{Binding obje}" ContentTemplate="{StaticResource persontemplate}" Grid.Row="0" Grid.Column="1"  />
        <ContentControl Content="{Binding obje}" ContentTemplate="{StaticResource persontemplate}" Grid.Row="0" Grid.Column="2"  />
        <ContentControl Content="{Binding obje}" ContentTemplate="{StaticResource persontemplate}" Grid.Row="1" Grid.Column="0"  />
        <ContentControl Content="{Binding obje}" ContentTemplate="{StaticResource 

关于如何做我想做的任何提示都是可以的。在此先感谢:)

0 个答案:

没有答案