DataTemplate

时间:2019-05-31 15:38:34

标签: c# wpf

我有5个内容,我希望在应用程序启动时显示在wrapPanels中,但是不幸的是,它无法按我的意愿工作。当我运行该应用程序时,只有最后一个项目“ Hiking”显示出来,其他项目则没有显示。请如何解决此问题。

<Grid>
    <ContentPresenter Content="{Binding com}">
        <ContentPresenter.ContentTemplate>                
            <DataTemplate>
                <WrapPanel Orientation="Horizontal">
                <WrapPanel  Orientation="Horizontal"  Margin="0,0" Height="342"  Width="228" Background="#FFF3F3F3">
                    <Image  Margin="13,40,13,10" Width="207" Height="228" 
                   ToolTip="Click To View More" PreviewMouseDown="Image_Clicked" Source="{Binding Image}" Stretch="UniformToFill"/>
                    <Border Margin="-10,-470,0,54" Height="54" BorderThickness="3"
                    Background="Yellow"  Padding="2"
                       HorizontalAlignment="Center" VerticalAlignment="Center" Width="244">
                        <TextBlock Text="{Binding EventName}" Margin="82,14,48,4" Height="26"
                           FontWeight="Black"/>
                    </Border>
                    <TextBlock Text="Attendees :" Margin="12,0,0,70" Width="73"/>
                    <TextBlock Text="Category :" Margin="-90,20,0,40" Width="54" Height="26"/>
                    <TextBlock Text="{Binding Attendees}" FontWeight="Bold" Margin="10,0,0,70" Width="48"/>
                    <TextBlock Text="{Binding Category}" FontWeight="Bold" Margin="-50,20,0,40" Width="58"/>
                    <TextBlock Text="Date :" Margin="12,-45,0,70" Width="73"/>
                    <TextBlock Text="{Binding Date}" FontWeight="Bold" Margin="10,-45,0,70"
                       Width="82" Height="26"/>
                </WrapPanel>
                </WrapPanel>
            </DataTemplate>
        </ContentPresenter.ContentTemplate>
    </ContentPresenter>                
</Grid>  

public partial class MainWindow : Window
{   
    public Company com { set; get; }
    public MainWindow()
    {
        InitializeComponent();
        com = new Company { EventName = "Code Talks" ,Attendees ="50" , Date = "25/oct./2019" };
        com = new Company { EventName = "Tic Tac", Attendees = "70", Date = "55/oct./2019" };
        com = new Company { EventName = "Loney Talks", Attendees = "30", Date = "5/oct./2019" };
        com = new Company { EventName = "Cofee Talks", Attendees = "50", Date = "25/oct./2019" };
        com = new Company { EventName = "Hiking ", Attendees = "40", Date = "75/oct./2019" };

        DataContext = this;
    }
}

public class Company
{
    public String EventName { get; set; }
    public String Attendees { get; set; }
    public String Date { get; set; }
}

}

我希望所有内容都显示出来,但只有徒步旅行节目

1 个答案:

答案 0 :(得分:1)

出现此问题的原因是因为您用每个新值覆盖了名为“ com”的变量。

com = new Company { EventName = "Code Talks" ,Attendees ="50" , Date = "25/oct./2019" };
com = new Company { EventName = "Tic Tac", Attendees = "70", Date = "55/oct./2019" };
com = new Company { EventName = "Loney Talks", Attendees = "30", Date = "5/oct./2019" };
com = new Company { EventName = "Cofee Talks", Attendees = "50", Date = "25/oct./2019" };
com = new Company { EventName = "Hiking ", Attendees = "40", Date = "75/oct./2019" }; 

这等效于尝试存储这样的几个整数:

x=5;
x=4;
x=1;

您将要使用某种数组并将每个项目添加到其中。如果您只打算使用这5个项目,则固定长度的数组会很好用:

Company[] coms = new Company[5];
coms[0] = new Company { EventName = "Code Talks" ,Attendees ="50" , Date = "25/oct./2019" };
coms[1] = new Company { EventName = "Tic Tac", Attendees = "70", Date = "55/oct./2019" };
coms[2] = new Company { EventName = "Loney Talks", Attendees = "30", Date = "5/oct./2019" };
coms[3] = new Company { EventName = "Cofee Talks", Attendees = "50", Date = "25/oct./2019" };
coms[4] = new Company { EventName = "Hiking ", Attendees = "40", Date = "75/oct./2019" };