DataTemplate for ListBox element is not binding at

2019-08-02 16:00发布

问题:

I have a ListBox element,
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,
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,
I only see: 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,
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;

回答1:

i dont like code definition for such thing so here is the xaml one

<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. so you would better understand what you need when you do binding with WPF.

a little example

create a class which will be your DataContext and put a public property for your List in it.

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

xaml.cs: set the DataContext for your View to your Viewmodel class

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

xaml: define your Bindings for your controls

<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>