【问题标题】:Class Object not visible after calling it to a ListBox from an ObservableCollection List. However it still exists and is selectable类对象从 ObservableCollection 列表调用到 ListBox 后不可见。但是它仍然存在并且可以选择
【发布时间】:2016-05-02 14:04:33
【问题描述】:

我正在填充一个充满汽车对象的列表框,这些汽车对象存储在 Observable Collection 上。

我从 LINQ 数据库中获取汽车的详细信息,将它们制成 Car 类对象并将它们存储到 Observable 集合 CarList 中,如下所示:

 var ListQuery = from c in db.Cars
                  select c;

 foreach (var car in ListQuery)
 {
     Car c1 = new Car(car.ID, car.Make, car.Model, car.Size);
     CarList.Add(c1);
 }

下面是 Car 类,包括它们要存储的两个字符串。

    public class Car 
    {
        public int ID { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
        public string Size { get; set; }

        public Car (int id, string make, string model, string size)
        {
            Size = size;
            Make = make;
            Model = model;
            ID = id;
        }

        public override string ToString()
        {
            return string.Format("{0} - {1}", Make, Model);
        }

        //This method is for displaying the selected listbox Item's details in a textbox.
        public string GetDetails()
        {
            return string.Format("CarID: {0}\nMake: {1}\nModel: {2}\n",
                    ID, Make, Model);

        }

最后,当按下按钮时,我用 CarList 项填充列表框。

private void btn_Search_Click(object sender, RoutedEventArgs e)
{
   lbx_CarList.ItemsSource = CarList;
}

对象填充列表框并且是可选择的,但是它们不可见。我原以为类中的ToString() 方法将是它们显示的内容,但事实并非如此。

下面显示了 WPF 窗口以及选择列表框中的项目后我看到的内容。 When code runs and listbox is populated with Car class objects from CarList Observable Collection

有人知道我缺少什么或如何显示对象项字符串吗?我试过了

lbx_CarList.ItemsSource = CarList.ToString()

CarList.ToList()

但无济于事。所以我希望这不是我错过的明显东西。任何帮助都会很棒!谢谢。

编辑:添加了 Xaml。我取出了一些额外的东西,所以只剩下与问题相关的代码。

<Window x:Class="FinalProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:FinalProject"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>

    <Label x:Name="label3" Content="Available Cars" HorizontalAlignment="Left" Margin="53,184,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
    <ListBox x:Name="lbx_CarList" HorizontalAlignment="Left" Height="99" Margin="53,210,0,0" VerticalAlignment="Top" Width="233" DisplayMemberPath="Description" SelectedValuePath="ID" SelectionChanged="lbx_CarList_SelectionChanged"/>
    <TextBlock x:Name="tbk_CarDetails" HorizontalAlignment="Left" Margin="319,210,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="99" Width="146" ClipToBounds="True"/>
    <Label x:Name="label4" Content="Selected Car" HorizontalAlignment="Left" Margin="319,184,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.421,-0.115" FontWeight="Bold"/>
    <Image x:Name="img_Car" HorizontalAlignment="Left" Height="100" Margin="319,56,0,0" VerticalAlignment="Top" Width="158"/>

    <Button x:Name="btn_Search" Content="Search" HorizontalAlignment="Left" Margin="167,159,0,0" VerticalAlignment="Top" Width="75" Click="btn_Search_Click"/>

</Grid>

【问题讨论】:

  • 您可能缺少绑定的路径部分。将 XAML 显示为 @Eldho 已询问的内容
  • 在上面添加了 Xaml。谢谢大家
  • 你的模型中的description在哪里?
  • 嗯..这不是你应该绑定 observablecollection 的方式,但乍一看你的列表框中你有DisplayMemberPath="Description"但我在你的汽车类中看不到该属性
  • 天哪。当我直接从数据库选择查询中填写结果时,我在代码的最开始就这样做了,我完全忘记了它。我是一个白痴。感谢一百万人,很抱歉浪费您的时间。

标签: c# sql wpf xaml observablecollection


【解决方案1】:

你的Model应该实现[INotifyPropertyChangedInterface][1]

public event PropertyChangedEventHandler PropertyChanged;

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


public class Car
{
    private string _make ;
    public string Make
    {
      get { return _make ; }
      set
      {
         _make = value;
         //This will update each time you set a value to the userinterface
         NotifyPropertyChanged("Make");
      }
  }
}

Xaml

 <ListView Name="ListView"ItemsSource="{Binding YourCollection}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <WrapPanel>
                    <TextBlock Text="{Binding Path=ID}"/>
                    <TextBlock Text="{Binding Path=Make}"/>
                </WrapPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

您可以使用Binding 显示列表或使用后面的代码显示项目。 看看MVVM会带来一些关于Binding的想法,如何在用户界面中绑定对象。

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多