请问,如果我没有提出好问题,但不知道该如何命名...
我有一个带有一些listView的项目,可以观察到collecterlection。当我右键单击我的listView,更改名称或其他参数时,ListView不会自动刷新直到我出门,然后再次打开。
这是我上下文的背后代码:
public class Contexte : INotifyPropertyChanged
{
private Affaire affaireSelectionnee;
public Affaire AffaireSelectionnee
{
get { return affaireSelectionnee; }
set
{
if (value == affaireSelectionnee) return;
affaireSelectionnee = value;
NotifyPropertyChanged("AffaireSelectionnee");
}
}
private ObservableCollection<Affaire> listeDesAffairesSelectionnees;
public ObservableCollection<Affaire> ListeDesAffairesSelectionnees
{
get { return listeDesAffairesSelectionnees; }
set { NotifyPropertyChanged(ref listeDesAffairesSelectionnees, value); }
}
private ObservableCollection<Phase> listeDesPhasesSelectionnees;
public ObservableCollection<Phase> ListeDesPhasesSelectionnees
{
get { return listeDesPhasesSelectionnees; }
set { NotifyPropertyChanged(ref listeDesPhasesSelectionnees, value); }
}
private ObservableCollection<Assemblage> listeDesAssemblagesSelectionnees;
public ObservableCollection<Assemblage> ListeDesAssemblagesSelectionnees
{
get { return listeDesAssemblagesSelectionnees; }
set { NotifyPropertyChanged(ref listeDesAssemblagesSelectionnees, value); }
}
private ObservableCollection<Repere> listeDesReperesSelectionnees;
public ObservableCollection<Repere> ListeDesReperesSelectionnees
{
get { return listeDesReperesSelectionnees; }
set { NotifyPropertyChanged(ref listeDesReperesSelectionnees, value); }
}
private ObservableCollection<Affaire> listeDesAffaires;
public ObservableCollection<Affaire> ListeDesAffaires
{
get { return listeDesAffaires; }
set { NotifyPropertyChanged(ref listeDesAffaires, value); }
}
private ObservableCollection<Phase> listeDesPhases;
public ObservableCollection<Phase> ListeDesPhases
{
get { return listeDesPhases; }
set { NotifyPropertyChanged(ref listeDesPhases, value); }
}
private ObservableCollection<Assemblage> listeDesAssemblages;
public ObservableCollection<Assemblage> ListeDesAssemblages
{
get { return listeDesAssemblages; }
set { NotifyPropertyChanged(ref listeDesAssemblages, value); }
}
private ObservableCollection<Repere> listeDesReperes;
public ObservableCollection<Repere> ListeDesReperes
{
get { return listeDesReperes; }
set { NotifyPropertyChanged(ref listeDesReperes, value); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string nomPropriete)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(nomPropriete));
}
private bool NotifyPropertyChanged<T>(ref T variable, T valeur, [CallerMemberName] string nomPropriete = null)
{
if (object.Equals(variable, valeur)) return false;
variable = valeur;
NotifyPropertyChanged(nomPropriete);
return true;
}
}
我加载程序时一次加载上下文:
DBConnect DataBase = new DBConnect();
string requete = "SELECT * FROM affaire ORDER BY ID";
List<Affaire> liste = DataBase.Select_affaire(requete, true);
contexte = new Contexte { ListeDesAffaires = new ObservableCollection<Affaire>(liste), ListeDesPhases = new ObservableCollection<Phase>(), ListeDesAssemblages = new ObservableCollection<Assemblage>(), ListeDesReperes = new ObservableCollection<Repere>(), AffaireSelectionnee = new Affaire(), ListeDesAffairesSelectionnees = new ObservableCollection<Affaire>(liste), ListeDesPhasesSelectionnees = new ObservableCollection<Phase>(), ListeDesAssemblagesSelectionnees = new ObservableCollection<Assemblage>(), ListeDesReperesSelectionnees = new ObservableCollection<Repere>() };
DataContext = contexte;
然后我的功能可能会更新属性:
foreach (Phase ph in contexte.ListeDesPhasesSelectionnees)
{
Phase ph_find = contexte.ListeDesPhases.First(s=>s==ph);
ph_find.Priorite = new_priorite;
}
ph_find.priorite已很好地更新了,我可观察到的集合" contexte.listedesphasses"也是如此,但是在listView上没有刷新。
edit :好吧,我可以解决添加listview1.items.refresh()...的问题。我不确定这是最正确的方法(不应该竞标应该刷新ListView自动?),但是目前它起作用
edit2 :
我的XAML代码(阶段的ListView):
<ListView x:Name="ListView2" ItemsSource="{Binding ListeDesPhases}" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="4" MouseDoubleClick="ListView_MouseDoubleClick" GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler" SelectionChanged="ListView_SelectionChanged" >
<ListView.View>
<GridView AllowsColumnReorder="true" x:Name="GridView2">
<GridViewColumn DisplayMemberBinding="{Binding ID}" Header="ID" Width="50"/>
<GridViewColumn DisplayMemberBinding= "{Binding NomPhase}" Header="{x:Static p:Resources.Nom}" Width="200"/>
<GridViewColumn DisplayMemberBinding="{Binding IdAffaire}" Header="{x:Static p:Resources.IdAffaire}" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding CommPhase}" Header="{x:Static p:Resources.Commentaire}" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Priorite}" Header="{x:Static p:Resources.Priorite}" Width="100"/>
</GridView>
</ListView.View>
</ListView>
我的阶段类:
public class Phase
{
public string NomPhase { get; set; }
public long IdAffaire { get; set; }
public string CommPhase { get; set; }
public int Priorite { get; set; }
public long ID { get; set; }
public List<Assemblage> ListAssemblages { get; set; }
public Phase()
{
this.NomPhase = "";
this.IdAffaire = 0;
this.CommPhase = "";
this.Priorite = 0;
this.ID = 0;
this.ListAssemblages = new List<Assemblage>();
}
...
}
edit3 :
试图按NetStep指示的修改,但仍然相同:
public ObservableCollection<Phase> ListeDesPhases
{
get { return listeDesPhases; }
set { NotifyPropertyChanged(ref listeDesPhases, value);
NotifyPropertyChanged("Priorite");
}
}
编辑4 :
好吧,我现在知道什么都没发生,我读了该课程http://www.wpf-tutorial.com/data-binding/responding/responding-to-changes/了解它...因此,Netstep给出的示例是好的(只是不了解RaisePropertyChanged(() => Priorite);
部分是什么?这是由于使用mvvmlight.net库吗?
public class Phase : INotifyPropertyChanged
{
private string nomPhase;
public string NomPhase
{
get { return this.nomPhase; }
set
{
if (this.nomPhase != value)
{
this.nomPhase = value;
this.NotifyPropertyChanged("NomPhase");
}
}
}
private int priorite;
public int Priorite
{
get { return this.priorite; }
set
{
if (this.priorite != value)
{
this.priorite = value;
this.NotifyPropertyChanged("Priorite");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
...//all of my other functions
}
所以这是我所做的,现在效果很好。
然后我有以下问题:我在4个ListView(合同,分包,阶段和细节)中显示了4个不同的对象。合同是"母级",它计算参数,但还包含分包合同列表。分包合同包含几个参数,一个阶段列表,每个阶段都包含一些参数,并带有详细信息列表。它们每个都显示在不同的ListView(4 ListView)中。我可以定义4个不同的ObservableCollection,还是有没有一种方法可以为所有"树"定义一个ObservableCollection,然后在我的ObservableCollection>?
要在UI中正确更新该字段,请确保阶段类也实现InotifyPropertyChanged在某种程度上并致电
NotifyPropertyChanged("Priorite")
在set访问器中。仅使用ObservableCollection是不够的,它就会通知仅添加/删除操作。正确 - listView1.items.refresh()在这种情况下是解决方法。希望这会有所帮助,否则请共享阶段类代码和您的XAML代码以获取更清晰的评论/答案。
这是基于MVVM光库的示例:
using GalaSoft.MvvmLight;
namespace WpfApp1
{
public class Phase : ViewModelBase
{
private int _priorite;
public int Priorite
{
get { return _priorite; }
set
{
_priorite = value;
RaisePropertyChanged(() => Priorite);
}
}
}
}
所有其余代码都可以保持不变。您还可以从ViewModelBase