简体   繁体   English

ListBox元素的DataTemplate根本没有绑定

[英]DataTemplate for ListBox element is not binding at all

I have a ListBox element, 我有一个ListBox元素,
which purpose is to show the users the activities, 目的是向用户展示活动,
that are registered on the Database 在数据库上注册的
so that they can choose from them to modify or delete them. 以便他们可以从中进行选择来修改或删除它们。

After consulting two very useful answers about using DataContext and DataTemplates , 在咨询了有关使用DataContextDataTemplates的两个非常有用的答案之后,
I decided to implement that knowledge in my project, 我决定在我的项目中实施这些知识,
unfortunately, it's not working. 不幸的是,它不起作用。

When I run it and I select the text on the ListBox , 当我运行它并选择ListBox上的文本时,
I only see: DataTemplate templ = new DataTemplate(typeof(Activities)); 我只看到: DataTemplate templ = new DataTemplate(typeof(Activities));
as its content, and I didn't mark it up as code, 作为其内容,而我没有将其标记为代码,
because I want to stress the fact that it appears as a string , 因为我想强调一下它以string出现的事实,
if you will. 如果可以的话。

I get that there could be more than one workaround for what I'm trying to achieve. 我得到的解决方案可能不止一种。
however I really want to understand this, as it appears to be very useful. 但是我真的很想了解这一点,因为它似乎非常有用。

Here's the code: 这是代码:

        //This is the connection instance to the database
        Connection c = new Connection();
        DataTemplate templ = new DataTemplate(
          typeof(Activities)
        );
        //The ListActivities method returns 
        //an ObservableCollection<Activities> list
        libTest.DataContext = c.ListActivities(
          objSem.getId()
        );
        libTest.SetBinding(
           ItemsControl.ItemsSourceProperty, new Binding()
        );

        FrameworkElementFactory sp = new FrameworkElementFactory(
          typeof(StackPanel)
        );
        sp.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
        sp.Name = "myTemplate";

        FrameworkElementFactory date = new FrameworkElementFactory(
          typeof(Label)
        );
        date.SetBinding(Label.ContentProperty, new Binding("date"));
        sp.AppendChild(date);

        FrameworkElementFactory nameAct = new FrameworkElementFactory(
          typeof(Label)
        );
        nameAct.SetBinding(Label.ContentProperty, new Binding("nameAct"));
        sp.AppendChild(nameAct);

        FrameworkElementFactory descr = new FrameworkElementFactory(
          typeof(Label)
        );
        descr.SetBinding(Label.ContentProperty, new Binding("descr"));
        sp.AppendChild(descr);

        FrameworkElementFactory quantity = new FrameworkElementFactory(typeof(Label));
        quantity.SetBinding(Label.ContentProperty, new Binding("quantity"));
        sp.AppendChild(quantity);

        templ.VisualTree = sp;

        libTest.ItemTemplate = templ;

i dont like code definition for such thing so here is the xaml one 我不喜欢这种东西的代码定义,所以这里是xaml

<DataTemplate DataType="{x:Type local:Activities}">
 <StackPanel Orientation="Horizontal">
   <Label Content="{Binding date}"/>
   <Label Content="{Binding nameAct}"/>
   <Label Content="{Binding descr}"/>
   <Label Content="{Binding quantity}"/>
 </StackPanel>
</DataTemplate>

just put this into your resources and all Activities will render like this 只需将其放入您的资源中,所有活动都会像这样渲染

pls read something more about binding in WPF, maybe MVVM stuff too. 请阅读有关WPF中绑定的更多信息,也许还有MVVM。 so you would better understand what you need when you do binding with WPF. 因此,与WPF绑定时,您会更好地了解需要什么。

a little example 一个小例子

create a class which will be your DataContext and put a public property for your List in it. 创建一个将作为您的DataContext的类,并在其中添加List的公共属性。

 public class SampleViewModel
 {
    public ObservableCollection<Activities> MyActivities {get;set;}
 }

xaml.cs: set the DataContext for your View to your Viewmodel class xaml.cs:将View的DataContext设置为Viewmodel类

 public partial class SampleWindow : Window  
 {
    private SampleViewModel _data;
    public SampleWindow()
    {
        _data = new SampleViewModel();
        InitializeComponent();
        this.DataContext = _data;
    }
 }  

xaml: define your Bindings for your controls xaml:为控件定义绑定

<ListBox ItemsSource="{Binding MyActivities}">
 <ListBox.ItemTemplate>
    <DataTemplate>
     <StackPanel Orientation="Horizontal">
      <Label Content="{Binding date}"/>
      <Label Content="{Binding nameAct}"/>
      <Label Content="{Binding descr}"/>
      <Label Content="{Binding quantity}"/>
    </StackPanel>
   </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

or 要么

<ListBox ItemsSource="{Binding MyActivities}">
 <ListBox.Resources>
    <DataTemplate DataType="{x:Type local:Activities}">
     <StackPanel Orientation="Horizontal">
      <Label Content="{Binding date}"/>
      <Label Content="{Binding nameAct}"/>
      <Label Content="{Binding descr}"/>
      <Label Content="{Binding quantity}"/>
    </StackPanel>
   </DataTemplate>
 </ListBox.Resources>
</ListBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM