ListBox元素的DataTemplate根本不绑定 [英] DataTemplate for ListBox element is not binding at all

查看:129
本文介绍了ListBox元素的DataTemplate根本不绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListBox元素,
目的是向用户显示活动,
在数据库上注册的
这样他们就可以从中进行选择来修改或删除它们.

咨询了关于使用 DataContext 的两个非常有用的答案之后数据模板
我决定在我的项目中实现这些知识,
不幸的是,它不起作用.

当我运行它并选择ListBox上的文本时,
我只看到:DataTemplate templ = new DataTemplate(typeof(Activities));
作为其内容,而我没有将其标记为代码,
因为我想强调一个事实,它显示为string
如果可以的话.

我知道我要实现的目标可能有多个解决方法.
但是我真的很想了解这一点,因为它似乎非常有用.

这是代码:

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

解决方案

我不喜欢这样的代码定义,所以这里是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>

只需将其放入您的资源中,所有活动"都将像这样呈现

pls阅读了有关WPF中绑定的更多信息,也许还有MVVM的东西.因此,与WPF绑定时,您会更好地了解需要什么.

一个小例子

创建一个将成为您的DataContext的类,并在其中添加List的公共属性.

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

xaml.cs:将View的DataContext设置为Viewmodel类

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

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>

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

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;

解决方案

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>

这篇关于ListBox元素的DataTemplate根本不绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆