EF在WPF列表框中使用值

时间:2016-01-13 09:33:29

标签: c# wpf entity-framework wcf listbox

我有一个数据表

  

tblFeed

     

Id

     

标题

     

内容

我使用此表在我的WPF应用程序中填充了一个Listbox。

我现在遇到的问题是使用事件的Id值,但ID会一直返回0

任何建议?

WCF

public List<Feed> GetFeed()
        {

            List<Feed> r = new List<Feed>();
            List<Feed> e;

            using (TruckDb db = new TruckDb())

                e = db.Feed.Where(x => x.Id != null).ToList();

            foreach (var a in e)
            {
                var feed = new Feed()
                {
                    Id = a.Id,
                    Title = a.Title,
                    Content = a.Content
                };

                r.Add(feed);

            }
            return r;
        }

WPF

   public async Task LoadFeeds()
            {
                TruckServiceClient TSC = new TruckServiceClient();
                try 
                {           
                  List<ClientItems> feeditems = new List<ClientItems>();

                  if (lbFeed.Items.Count <= 0)

                     foreach (var item in await TSC.GetFeedAsync())
                    {
                        feeditems.Add(new ClientItems
                        {
                            FId = item.Id,
                            FTitle = item.Title,
                            FContent = item.Content
                        });
                    }

                    lbFeed.ItemsSource = (feeditems.ToArray());
                    lbFeed.DisplayMemberPath = "FTitle";



                }
                catch (Exception)
                {

                    throw;
                }
                        }


public class ClientItems
    {

            public int FId { get; set; }

            public string FTitle { get; set; }

            public string FContent { get; set; }


        public override string ToString()
        {
            return FTitle;
        }

    }

删除活动

WCF

 private void bnFeedDel_Click(object sender, RoutedEventArgs e)
        {
            TruckServiceClient service = new TruckServiceClient();


                service.DelFeedAsync(new FeedView
                {
                    Id = lbFeed.SelectedIndex
                });

         }

WPF

 public void DelFeed(FeedView feedview)
        {
            using (var result = new TruckDb())
            {
                var t = new Feed
                {
                    Id = feedview.Id
                };
                result.Feed.Remove(t);
                result.SaveChanges();
            }
        }

1 个答案:

答案 0 :(得分:0)

bnFeedDel_Click方法中,您正在执行此操作:

Id = lbFeed.SelectedIndex

我认为这是您的问题,因为您不希望将Id设置为SelectedIndex值,而是:

[经过讨论后编辑]

SelectedValuePath内设置LoadFeeds

lbFeed.SelectedValuePath = "FId";

使用SelectedValue代替SelectedIndex

private void bnFeedDel_Click(object sender, RoutedEventArgs e)
{
    TruckServiceClient service = new TruckServiceClient();

    service.DelFeedAsync(new FeedView
    {
        // Of course you may want to check for nulls etc...
        Id = (int)lbFeed.SelectedValue;
    });
 }
删除记录前

Also, you should use DbSet.Attatch()

public void DelFeed(FeedView feedview)
{
    using (var result = new TruckDb())
    {
        var t = new Feed
        {
            Id = feedview.Id
        };
        result.Feed.Attatch(t);
        result.Feed.Remove(t);
        result.SaveChanges();
    }
}