如何将ListBox控件绑定到WPF中的List?

时间:2011-03-05 22:29:08

标签: c# .net wpf listbox binding

我想在列表框和.NET列表之间创建双向绑定。

在我的GUI中,我有一个列表框,一个文本框以及添加和删除按钮。 列表框显示汽车,我的目标是在.Net汽车列表和列表框之间创建双向绑定:当用户将汽车输入文本框时,它仅在.Net列表中更新,列表框为自动更新。

当用户按下GUI“删除”按钮时,汽车将从GUI中删除,.Net列表会自动更新。

我已经开始编写xaml代码了,但我发现我实际上并不知道如何在两边进行绑定(c#和xaml):

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="369" Loaded="Window_Loaded">
    <Window.Resources>
        <ObjectDataProvider x:Key="carsData" 
                        ObjectType="{x:Type c:Window1}" />
    </Window.Resources>
    <Grid Width="332">
        <ListBox Margin="10,62,0,100" Name="myListBox" HorizontalAlignment="Left" Width="120" ItemsSource="{Binding Source={StaticResource CarsData}}"/>
        <Button Height="23" Margin="66,0,0,65" Name="addBtn" VerticalAlignment="Bottom" Click="addBtn_Click" HorizontalAlignment="Left" Width="64">add</Button>
        <TextBox Margin="10,0,0,64.48" Name="myTextBox" Height="23" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="47" />
        <Button Height="23" Margin="66,0,0,33" Name="removeButton" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="64" Click="removeButton_Click">Remove</Button>
    </Grid>
</Window>

有我的c#代码:

public partial class Window1 : Window
{
    MyModel listMgr;
    ObservableCollection<Car> carList;

    public Window1()
    {
        InitializeComponent();
        listMgr = new MyModel();
    }

    private void addBtn_Click(object sender, RoutedEventArgs e)
    {
        listMgr.add(new Car(0, myTextBox.Text, 2011));

    }

    private void removeButton_Click(object sender, RoutedEventArgs e)
    {
        //myListBox.Items.RemoveAt(0);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        carList = listMgr.getList();
        myListBox.DataContext = carList;
        //secondListBox.DataContext = carList;
    }
}

1 个答案:

答案 0 :(得分:3)

这是一个快速版本,您需要添加代码来检查汽车是否被选中等等。

要查看您的Car数据,您需要定义数据模板。在示例中,它只是一个简单的名称,但您可以更改文本颜色,字体大小,添加更多字段等。

最好在添加/删除汽车时处理列表,而不是直接在ListBox上。

XAML:

<Window x:Class="cars.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <DataTemplate x:Key="car_template" DataType="Car">
        <TextBlock Text="{Binding name}"/>
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <ListBox x:Name="cars_box" Margin="5" ItemsSource="{Binding}" ItemTemplate="{StaticResource car_template}"/>
    <TextBox x:Name="new_car_box" Margin="5"/>
    <Button Content="add" Click="add_car" Margin="5"/>
    <Button Content="delete" Click="delete_car" Margin="5"/>
</StackPanel>

C#:

using System.Collections.ObjectModel;
using System.Windows;    
public partial class MainWindow : Window
{
    ObservableCollection<Car> cars = new ObservableCollection<Car>();
    public MainWindow()
    {
        InitializeComponent();            
        cars.Add(new Car("Volvo"));
        cars.Add(new Car("Ferrari"));
        cars_box.DataContext = cars;
    }

    private void add_car(object sender, RoutedEventArgs e)
    {
        cars.Add(new Car(new_car_box.Text));
    }

    private void delete_car(object sender, RoutedEventArgs e)
    {
        cars.Remove((cars_box.SelectedItem as Car));
    }
}

public class Car
{
    public string name { get; set; }
    public Car(string _name)
    {
        this.name = _name;
    }
}