2012-03-09 47 views
1

我正在为我的城市比赛的一个小项目工作..我只是打砖墙。事情是:我在Blend中创建一个userControl(让我们说一个画布,有一个矩形..一个文本块和一个图像)。我的问题是,我不能将此添加到WPF中的listboxitem代码.addin usercontrol一个接一个在设计器中似乎工作..但软件将工作列表框的项目数量可变。将用户控件添加到列表框

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尝试添加一些按钮,它的伟大工程......但似乎恨我的用户。

我在等你的想法如何解决这个问题,如果你有什么其他最好在我的情况下使用和如何..它会很好,谢谢!

+0

你得到什么错误? – AlexDrenea 2012-03-09 16:42:48

+1

我正确地认为你试图创建一个具有多个属性的项目显示的列表框(图像+文本块)?如果是这样,那么你不必创建一个新的用户控件。您可以通过创建一个数据模板,并将列表框的itemTemplate设置为dataTemplate来完成。我有一些代码,如果这是你想要做的 – emybob 2012-03-09 16:43:03

+0

标签**不要**属于标题。 – 2012-03-09 22:53:31

回答

2

好的,这里有一些实际的代码可以帮助你。 我将使用几个您可能想要进一步研究的WPF概念:DataBinding,DataTemplates,ImageSources,ObservableCollections

首先,您需要创建(如果您还没有)您的问题的基础类。你可以得到最简单的将是这样的:

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

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

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 =这是非常重要的,否则你的数据绑定将无法正常工作。

在您的设计区域中,创建一个列表并绑定到您创建的Questions集合。问题显示在列表中的方式由以下的“ItemTemlpate”驱动。

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

最后,如果你不知道如何得到一个ImageSource的,当你在你的项目中的JPG或PNG文件:

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

    return res; 
} 
0

您需要创建控件的集合(例如List)并将集合绑定到ListBox。

2

right处理这种情况的方法是通过一个数据模型和你的问题集合。然后将您的ListBox.ItemsSource绑定到集合并提供一个使用您的UserControl的DataTemplate。

1

使用ListBox的ItemTemplate来定义对象的每个实例的样子,然后将ListBox的ItemsSource绑定到该类型的集合。