用户控件添加到列表框 [英] Add userControl to listbox

查看:111
本文介绍了用户控件添加到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的city..and比赛一个小项目的工作,我只是打了一个砖头wall.The事情是:我创建在Blend一个用户控件(比方说一个画布,在至极我有一个reactangle。 .A文本块和图像)。我的问题是,我不能在设计一个code.Addin的用户控件之一时,此添加到WPF的ListBoxItem中似乎work..but该软件将与可变数量的工作项的列表框。

I am working on a little project for a contest in my city..and i just hit a brick wall.The thing is: i am creating a userControl in Blend(let's say a canvas,in wich i have a reactangle..a textblock and an image).My problem is that i can not add this to the listboxitem in WPF by code.Addin the userControl one by one in the designer seems to work..but the software is going to work with a variable number of items for the listbox.

    private void mainPane1_Loaded(object sender, RoutedEventArgs e)
    {
         MessageBox.Show("layout updated");
        questions cq;
        Button btn;

        for (int i = 1; i <= 10; i++)
        {
            btn = new Button();
            btn.Content = "intreb" + i.ToString();
            cq = new questions();
            Canvas.SetZIndex(cq, 17);
            //cq.questionHolder.Text = "intrebare" + i.ToString();
            listaintrebari.Items.Add(cq);
            MessageBox.Show("intrebare" + i.ToString());
            listaintrebari.Items.Add(btn);
            //MessageBox.Show("layout updated");

        }

    }



问题是我的用户控件和listaintrebari是listbox.I尝试添加一些按钮,它的伟大工程......但似乎恨我的用户。

questions is my UserControl and listaintrebari is the listbox.I tried to add some buttons and it works great...but it seems to hate my userControl.

我在等待你的想法如何解决这个问题,如果您有任何sugestions什么其他的最好是在我的情况下使用和how..it会great.Thank你!

I am waiting for your thoughts on how to resolve this issue, and if you have any sugestions on what other is best to use in my situation and how..it would be great.Thank you!

推荐答案

好吧,这里的一些实际的代码,可以帮助你。
我会用,你可能想进一步学习几个WPF概念:数据绑定,的DataTemplates,ImageSources,ObservableCollections

Ok, here's some actual code that might help you out. I will be using several WPF concepts that you might want to study further : DataBinding, DataTemplates, ImageSources, ObservableCollections

首先,你需要创建(如果你不'吨有它尚未)你的问题的基础类。你可以得到的最简单的将是这样的:

First you need to create (if you don't have it yet) an underlying class for your Questions. The simplest you can get would be something like this :

 internal class Question
 {
     public ImageSource QuestionImage { get; set; }
     public string QuestionText { get; set; }
 }

然后在后面的屏幕的代码(是的,我们是不是在MVVM尚未),你应该建立问题的一个ObservableCollection,并与您的问题
pouplate他们我有水木清华这样的:

Then in your screen's code behind (yes, we are not at MVVM yet), you should create an ObservableCollection of Question and pouplate them with your questions I have smth like this:

public ObservableCollection<Question> Questions { get; private set; }
public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;
    Questions = new ObservableCollection<Question>();

    for (int i = 1; i <= 10; i++)
    {
        var newQ = new Question { QuestionText = "intrebarea " + i.ToString(), QuestionImage = _get your image as a ImageSource here_ };
        Questions.Add(newQ);
    }
}




  • 的this.DataContext =这是非常重要的,否则你的数据绑定将无法工作。

  • 在设计区域,创建列表和绑定的给你创建的问题集合。 。问题是显示在列表的方式由ItemTemlpate驱动如下

    In your design area, create a list and bind it to the Questions collection you created. The way the question is displayed in the list is driven by the "ItemTemlpate" as below.

    <ListBox ItemsSource="{Binding Questions}">
      <ListBox.ItemTemplate>
        <StackPanel>
          <Image Source="{Binding QuestionImage}" Height="20" Width="20"/>
          <TextBlock Margin="5" Text="{Binding QuestionText}" />
        </StackPanel>
      </ListBox.ItemTemplate>
    </ListBox>
    




    • 您可以与您的用户控件的内容或事件替换我有用户控件本身,但一定要保持绑定的对象在你的问题类。

    • 就像我上面说的,很多东西可能不意义在这一点上,以便确保您了解他们:什么是数据绑定,什么是DataContext的,什么是一个ObservableCollection。此外,尝试寻找MVVM当你得到一个机会...

      Like I said above, many things might not make sense at this point so make sure you read about them : What is a data Binding, What is a DataContext, What is an ObservableCollection. Also, try looking at MVVM when you get a chance...

      最后,如果您不确定如何当你有一个JPG或PNG文件,以获得您的ImageSource项目:

      Lastly, if you are unsure how to get an ImageSource when you have a jpg or png file in your project:

      public ImageSource GetImagesource(string location)
      {
        var res = new BitmapImage()
        res.BeginInit();
        res.UriSource = new Uri("pack://application:,,,/ApplicationName;component/" + location);
        res.EndInit();
      
        return res;
      }
      

      这篇关于用户控件添加到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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