对不起,如果我没有提出好问题,但不知道如何更好地命名......
我有一个带有一些ListView的项目,绑定到ObservableCollection。 当我右键单击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已更新,我的Observable Collection&#34; contexte.ListeDesPhases&#34;,但没有在ListView上进行刷新。
编辑:我可以解决添加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>();
}
...
}
编辑3 :
尝试按照Netstep的说明进行修改,但仍然相同:
public ObservableCollection<Phase> ListeDesPhases
{
get { return listeDesPhases; }
set { NotifyPropertyChanged(ref listeDesPhases, value);
NotifyPropertyChanged("Priorite");
}
}
编辑4 :
好吧,我现在明白没有发生任何事情,我读了http://www.wpf-tutorial.com/data-binding/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个不同的对象。 合同是&#34; Mother Class&#34;,它计算参数,但也包含分包合同列表。分包含包含多个参数和阶段列表,每个阶段包含一些参数,以及详细信息列表。 它们中的每一个都显示在不同的ListView(4 ListView)中。 我可以定义4个不同的ObservableCollection,或者是否有一种方法只为所有&#34;树&#34;定义一个ObservableCollection,然后绑定我的ObservableCollection的参数&gt;?
答案 0 :(得分:1)
要在UI中正确更新字段,请确保Phase类也实现了INotifyPropertyChanged并调用
Set tokens = callerSubject.getPrivateCredentials();
for (Object o : tokens) {
if(o instanceof SingleSignonToken) {
SingleSignonToken ssoToken = (SingleSignonToken)o;
System.out.println("getName: " + ssoToken.getName()+"<BR>");
if("LtpaToken".equals(ssoToken.getName())){
System.out.println("getExpiration: " + ssoToken.getExpiration()+"<BR>");
}
}
}
在set访问器中。仅使用ObservableCollection是不够的,它只处理通知添加/删除操作。你是对的 - ListView1.Items.Refresh()在这种情况下是一种解决方法。 希望这会有所帮助,否则请分享阶段类代码和您的xaml代码以获得更清晰的评论/答案。
以下是基于MVVM光库的示例:
<?php
$a = 'C8';
$b = 0xC8;
$c = intval($a, 16);
$b === $c; // true
所有其余代码可以保持不变。您还可以从ViewModelBase
继承Context类