【问题标题】:ListVIew Data BindingsListVIew 数据绑定
【发布时间】:2015-06-11 17:58:14
【问题描述】:

我正在尝试显示有关汽车列表的信息。该列表存储在 CarLibrary 中,我无法获取显示在相应标题下方的信息。

这是我的 xaml:

        <TabItem Header="Overview">
            <Grid>
                <ListView Margin="10" ItemsSource="{Binding CarLibrary}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Picture" Width="100" DisplayMemberBinding="{Binding PicturePath}"/>
                            <GridViewColumn Header="Year" Width="100" DisplayMemberBinding="{Binding Year}"/>
                            <GridViewColumn Header="Make" Width="100" DisplayMemberBinding="{Binding Make}"/>
                            <GridViewColumn Header="Model" Width="100" DisplayMemberBinding="{Binding Model}"/>
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>

        </TabItem>

这里是c#中视图模型的相关代码,每个Car对象都有Make、Model、Year和PicturePath的属性:

namespace CarApp
{
public class Carvm : INotifyPropertyChanged
{
    private CarLib carLibrary;
    public CarLib CarLibrary
    {
        get { return carLibrary; }
        set
        { 
            carLibrary = value;
            NotifyPropertyChanged("CarLibrary");
        }
    }

    public Carvm()
    {
        carLibrary = new CarLib();
        newCar = new Car();
        currentCar = new Car();
        rmCommand = new DeleteCommand(carLibrary);
        touchCommand = new AddCommand(carLibrary, newCar);

        //load list from disk on startup
        carLibrary.ReadList();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}
}

这里是 CarLib:

namespace CarApp
{
public class CarLib : INotifyPropertyChanged
{
    private ObservableCollection<Car> list;

    public ObservableCollection<Car> List 
    {
        get { return list; }
        set {
            list = value;
            NotifyPropertyChanged("List");
        }
    }

    public CarLib()
    {
        List = new ObservableCollection<Car>();
    }

    /// <summary>
    /// Deserializes list of cars and returns the list to user
    /// </summary>
    public void ReadList()
    {
        //create local list to hold data
        ObservableCollection<Car> carList = new ObservableCollection<Car>();

        try
        {
            using (Stream stream = File.Open("data.bin", FileMode.OpenOrCreate))
            {
                BinaryFormatter bin = new BinaryFormatter();


                try
                {
                    //point List to deserialized stream
                    List = (ObservableCollection<Car>)bin.Deserialize(stream);
                }
                catch (SerializationException)
                {
                    Console.WriteLine("The list is empty.");
                }

            }
        }

        catch (SerializationException e)
        {
            if (e.Source != null)
            {
                Console.WriteLine("Empty List");
            }
        }
    }

    /// <summary>
    /// Serialize list supplied by the user and write to disk
    /// </summary>
    public void WriteList()
    {
        try
        {
            using (Stream stream = File.Open("data.bin", FileMode.Create))
            {
                BinaryFormatter bin = new BinaryFormatter();

                //write list to disk
                bin.Serialize(stream, List);
            }
        }
        catch (IOException)
        {
            Console.WriteLine("Empty List");
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}
}

【问题讨论】:

  • 是您的CarLibObservableCollection 还是Car?否则使用ObservableCollection&lt;Car&gt;
  • 发布CarLib的定义
  • @DLeh 我发布了 CarLib.cs 文件。 CarLib 是 Car 的 ObservableCollection。谢谢。
  • @rageit CarLib 是 Car 的 ObservableCollection

标签: c# xaml listview data-binding


【解决方案1】:

问题是我在 ListView 中设置了 ItemsSource="{Binding CarLibrary}",而它应该是:ItemsSource="{Binding CarLibrary.List}"。谢谢大家的帮助。

【讨论】:

    【解决方案2】:

    CarLibrary 定义为ObservableCollectionCar 会很好:

    public ObservableCollection<Car> CarLibrary {get; set;}
    

    然后使用CarLib.ReadList() 方法在构造函数中加载集合。假设ReadList 返回IEnumerable&lt;Car&gt;,您可以使用ToObservalbe 扩展方法将其转换为ObservableCollection

    CarLibrary = new CarLib().ReadList().ToObservable();
    

    您的 XAML 视图对我来说看起来不错,应该会呈现您的汽车列表。

    【讨论】:

      猜你喜欢
      • 2011-01-01
      • 2011-02-07
      • 2011-04-30
      • 2016-08-01
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      • 2013-06-04
      相关资源
      最近更新 更多