更改后绑定dymcly listBox [英] Bind dymcly listBox after changes

查看:77
本文介绍了更改后绑定dymcly listBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的类:

Hi I have a class that looks like this:

public class ListProduct{
 public string productName { get; set;}
 public double pricePerUnit { get; set;}
 public int number {get; set;}
 public double totalPrice {get; set;}
}

,我有一个ListProducts,它是ListProduct的集合.

当我单击按钮时,我将ListProduct添加到ListProducts中,并以这种方式更新listview:

and I have ListProducts that is a collection of ListProduct.

When I click on a button I add a ListProduct to my ListProducts and I update my listview this way:

myListView.datasource= null;
myListView.datasource= ListProducts ;

我知道这太愚蠢了,所以我在问是否还有另一个为什么要这样做?我的意思是
在我的列表中,当我将新产品添加到ListProducts中时,我有5种产品(这意味着在ListProducts中我有5种产品),我希望myListView动态更新.

It''s too stupid I know so I''m asking if there is another why to do that? I mean
in my list I have 5 product (it mean that in my ListProducts I have 5 product) when I add a new product into my ListProducts I''d like myListView to update dynamically.

推荐答案

感谢伟大的工作:)
很抱歉抽出宝贵的时间
但是您知道我是WPF的初学者,我想在这里生存下来:)
Thank''s great work :)
sorry for taking to much from your time
but you know I''m beginner in WPF I''m trying to survive here :)


执行此操作的方法是使用自动通知附件ItemControl的内容,该控件在更新时已更新一个项目被添加或删除.如果仅.NET提供了某些观察到的集合;天哪,您认为.NET实际上可以提供这样的东西吗? ObservableCollection<ListProduct>之类的东西. Google for ObservableCollection和WPF,您会发现很多有关如何执行此操作的示例.
The way to do this is to use something that automatically notifies attached ItemControls that it has updated when an item is added or removed. If only .NET provided a collection that was observed by something; gosh do you think that .NET could actually provided something like this; something like an ObservableCollection<ListProduct>. Google for ObservableCollection and WPF and you''ll find shed loads of examples on how to do this.


感谢您的帮助
我有这个代码
当我单击按钮时,如果该元素存在于列表中,那么我会修改数字(如果不存在的话),将其添加

当我添加elemnt myList时,更新了,但是当我第二次单击以修改数字时,它不会改变任何爆炸效果吗?
< pre> ObservableCollection< ListProduct> myList = new ObservableCollection< ListProduct>();
私有void Button_Click(对象发送者,RoutedEventArgs e)
{
int index = myList.IndexOf((来自myList中的par
其中par.name ="..."
选择参数).FirstOrDefault());
if(index!= -1)
{
myList [index] .number ++;

}
其他
{
myList.Add(new ListProduct {name ="...",number = 1});
}
}

公共类ListProduct {
公用字符串productName {get;设置;}
public double pricePerUnit {get;设置;}
public int number {get;设置;}
public double totalPrice {get;设置;}
}</pre>
代码Xaml
< ListView名称="ListView12" ItemsSource ="{Binding}">
< ListView.View>
< GridView>
< GridViewColumn标头=名称"
DisplayMemberBinding ="{绑定名称}">
< GridViewColumn标头=名称"
DisplayMemberBinding ="{绑定号码}">
</GridView>
< ListView.View>
</ListView>
Thanks for you help
I have this code
when I click on the button if the element exist on my list so I just modify the number if not I add it

when I add the elemnt myList is updated but when I click second time to modfy number it does not changes any explination please?
<pre>ObservableCollection<ListProduct> myList = new ObservableCollection<ListProduct>();
private void Button_Click(object sender, RoutedEventArgs e)
{
int index = myList.IndexOf((from par in myList
where par.name ="..."
select par).FirstOrDefault());
if(index != -1)
{
myList[index].number++;

}
else
{
myList.Add(new ListProduct { name = "...", number = 1 });
}
}

public class ListProduct{
public string productName { get; set;}
public double pricePerUnit { get; set;}
public int number {get; set;}
public double totalPrice {get; set;}
}</pre>
code Xaml
<ListView Name="ListView12" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="name"
DisplayMemberBinding="{Binding name}">
<GridViewColumn Header="name"
DisplayMemberBinding="{Binding number}">
</GridView>
<ListView.View>
</ListView>


这篇关于更改后绑定dymcly listBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆