将其他项添加到Custom ListBox / ListView以及用户如何添加自己的项

时间:2013-09-24 19:42:48

标签: c# wpf listview listbox

尝试学习WPF,这让我疯狂。所以我想尝试做一些像我在这里发布的图片。但我只能将组合框,文本,复选框放在彼此的顶部,而不是并排...我怎么能这样做?

此外,我想出了如果用户按下按钮,如何将“文本”添加到列表框中,但是我需要它以便在文本框中添加文本时。组合框,复选框和按钮随之添加。但我不知道该怎么做。

我假设我必须为这些事情上课。但是,如果我在XAML中编码,我该怎么做?这个WPF对我来说很困惑。

<ListBox HorizontalAlignment="Left" Height="195" Margin="25,345,0,0" VerticalAlignment="Top" Width="650">
                <ListBoxItem>
                        <StackPanel Orientation="Horizontal" Height="45"> <!--Stacks Items Horizontally-->
                            <ComboBox Width="100" Height="30">
                                <ComboBoxItem IsSelected="True">DirecTV</ComboBoxItem>
                                <ComboBoxItem>Hyundai</ComboBoxItem>
                                <ComboBoxItem>None</ComboBoxItem>
                            </ComboBox>
                            <TextBox Width="445" Height="30" Text="Follow RedZone on Twitter" VerticalContentAlignment="Center"/>
                            <CheckBox IsChecked="True" VerticalAlignment="Center">
                                <CheckBox.LayoutTransform>
                                    <ScaleTransform ScaleX="1.5" ScaleY="1.5"></ScaleTransform>
                                </CheckBox.LayoutTransform>
                            </CheckBox>
                        <Button Content="Delete"  Height="Auto" Width="Auto" HorizontalAlignment="Right" VerticalAlignment="Top" VerticalContentAlignment="Top"/>
                    </StackPanel>
                </ListBoxItem>
            </ListBox>

1 个答案:

答案 0 :(得分:3)

定义一个容纳项目的类

public class myListBoxRow
{
    public string comboBoxSelection {get;set;}
    public string textBlockText {get;set;}
    public bool checkBoxChecked {get;set;}
}

现在在某处定义ObservableCollectionList(通常在ViewModel中)

public ObservableCollection myListBoxRows<myListBoxRow> {get;set}

然后将ListBox的{​​{1}}绑定到您的收藏集

ItemsSource

要获得所需的控件,请为ListBox定义ItemTemplate

<ListBox ItemsSource="{Binding myListBoxRows}" .../>

在WPF中研究MVVM,因为到处都有大量的这样的例子。将这些联系在一起的关键部分是listbox / listview的数据窗口

相关问题