【问题标题】:Adding multiple content to a listbox. C#, WPF将多个内容添加到列表框。 C#,WPF
【发布时间】:2014-02-05 16:27:31
【问题描述】:

我的 WPF 应用程序有一个包含多个项目的列表框。在每个项目上,我们都有一个图像和一个文本框。

请参阅下面的 XAML:

      <ListBox x:Name="lstMagic" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="10,200,10,0" Width="Auto" Height="558" Background="{x:Null}" BorderBrush="Transparent">

            <ListBoxItem>
                <StackPanel Orientation="Horizontal">
                    <Image Source="images/plates/pig.jpg" Margin="0,0,5,0" Width="72"/>
                    <TextBox IsReadOnly="True"  Background="{x:Null}" Foreground="White" Width="912">Some Magic is going to happen</TextBox>
                </StackPanel>
            </ListBoxItem>

所以列表按预期从猪开始。在猪面前,他成功加载了 pig.jpg。

在那个表单部分旁边,我有一个按钮。我可以在那个按钮上做一些魔术来添加一个新的 listBoxItem。

        private void btnDoMagic(object sender, RoutedEventArgs e)
    {
        ListBoxItem newMagic = new ListBoxItem();
        Image imageCurrent = new Image();
        imageCurrent.Source = new BitmapImage(new Uri("images/plates/someOtherImage.jpg", UriKind.RelativeOrAbsolute));
        newMagic.Content = imageCurrent;
        lstCardapio.Items.Add(newMagic);
    }
}

此功能有效,并将图像成功添加到列表中!但我有一些问题: 我怎样才能像我的第一个 XAML 一样制作?一个图像和一个文本框?如何在我的按钮事件中添加这些?

我需要这样做,因为我们将从数据库中获取这些项目,因此我们必须动态加载这些项目。

【问题讨论】:

  • 不要在 WPF 的过程代码中创建或操作 UI 元素。这就是 XAML 的用途。使用ObservableCollection&lt;T&gt; 创建一个适当的 ViewModel 并将ListBox.ItemsSource 绑定到该集合。然后使用 ListBox.ItemTemplate 属性定义每个项目的 UI 外观。
  • 因为 WPF 不打算以这种方式使用。你最终会得到很多不需要的、难以维护的代码。创建一个合适的 ViewModel,你所有的问题都会神奇地消失。

标签: c# wpf xaml listbox


【解决方案1】:

如果我理解正确,您希望完全复制您的 XAML 层次结构,但代码除外,然后将其添加到 ListBox?

如果你想直接在代码中创建控件,你可以使用这样的东西:

    // This method creates your controls and returns a listBoxItem with the specified content.
    private ListBoxItem createObject(String imgSource, String text)
    {
        var newImage = new Image() 
            { 
                Source = new BitmapImage(new Uri(imgSource, UriKind.RelativeOrAbsolute)), 
                Width = 72, 
                Margin = new Thickness(0, 0, 5, 0) 
            };
        var newTextBox = new TextBox() 
            { 
                IsReadOnly = true, 
                Background = Brushes.Transparent, 
                Foreground = Brushes.White, 
                Width = 912, 
                Text = text 
            };

        var newStackPanel = new StackPanel() { Orientation = Orientation.Horizontal };

        newStackPanel.Children.Add(newImage);
        newStackPanel.Children.Add(newTextBox);

        return new ListBoxItem() { Content = newStackPanel };
    }


    private void btnDoMagic(object sender, RoutedEventArgs e)
    {
        // Get your URI and textbox text here, and use them as arguments below.

        1stMagic.Items.Add(createObject("Sample URI", "Sample Textbox Content"));
    }

当然,它不是特别整洁,并且要求您在修改它时知道它对应的 XAML 是什么,并且一旦您离开事件处理程序,您就会失去对控件的跟踪(因为它们没有名称或方法可以直接得到他们),但它应该工作。

如果您想进一步修改创建的对象,您可以遍历 1stMagic.Items,或者您可以将创建的控件添加到您在代码中维护的私有内部列表并以这种方式管理它们。

【讨论】:

  • 解决了。但正如@HighCore 所说,最好创建我的绑定......我正在学习这样做。无论如何,该答案本身就解决了问题,并且很有帮助!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
  • 2017-10-28
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多