[英]WPF Bind to Observable Collection within XAML declared class instance
我在App.xaml中定義了一個類的實例,因此可以從應用程序中的任何位置訪問此實例。
班級
using Common;
using System.Collections.ObjectModel;
using System.Windows;
namespace PhotoManagement
{
public class LoggedUsers : NotifyUIBase
{
public ObservableCollection<Logged> Logged = new ObservableCollection<Logged>();
public void Add(Logged user)
{
Logged.Add(user);
UpdateView(); //Check the List for users
RaisePropertyChanged("Logged"); //Notify View binding there is a change
}
//Check if there is a user logged in
public void UpdateView()
{
if (Logged.Count < 1)
{
MessageBox.Show("No one logged in!");
//No user is logged in, navigate to login view
//views[0].NavigateExecute(); Does not work, as views object is out of scope
}
else
{
MessageBox.Show("Someone is logged in!");
//User is logged in, navigate to home view
}
}
}
}
如下所示,我在XAML中設置了該類的實例,並給出了一個Key:
<Application x:Class="PhotoManagement.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PhotoManagement"
StartupUri="/Views/Main/MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<local:LoggedUsers x:Key="LoggedUsers" />
</ResourceDictionary>
</Application.Resources>
</Application>
我檢查了項目是否已添加到列表中,因為在調用Add()方法時,它還會運行UpdateView()方法,該方法向我顯示有一個值。
但是我正在努力在ListView中顯示項目,或者沒有通知ListView更新,但是應該如此!
<ListView ItemsSource="{Binding Logged, Source={StaticResource LoggedUsers}}" Width="100" Height="100">
<ListViewItem>
<TextBlock Text="{Binding FirstName}" />
</ListViewItem>
</ListView>
即使將項目添加到ListView中,上述ListView仍然為空。
這也是Logged類:
namespace PhotoManagement
{
public class Logged
{
public string ClearPassword { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public long UID { get; set; }
public string Email { get; set; }
//Display basic statistics
public int ThisDayImageCount { get; set; }
public int ThisDaySaleCount { get; set; }
public Logged()
{
//Update the stats when instigated
UpdateStats();
}
//Update the stats
public void UpdateStats()
{
}
}
}
代碼有多個問題。
Logged
應該是屬性,而不是字段。 您無法綁定到字段。
public ObservableCollection<Logged> Logged { get; set; }
還向LoggedUsers
添加一個構造LoggedUsers
以初始化屬性(或使用C#6的自動屬性初始化器):
public LoggedUsers() { Logged = new ObservableCollection<Logged>(); }
XAML應該如下所示:
<ListView ItemsSource="{Binding Logged, Source={StaticResource LoggedUsers}}" Width="100" Height="100"> <ListView.ItemTemplate> <DataTemplate DataType="local:Logged"> <TextBlock Text="{Binding FirstName}" /> </DataTemplate> </ListView.ItemTemplate> </ListView>
我認為您不需要RaisePropertyChanged("Logged");
。 ObservableCollection
通知其內容的更改。
進行這些修改之后,現在,如果您在應用程序中的某個位置執行此操作:
var loggedUsers = FindResource("LoggedUsers") as LoggedUsers;
if (loggedUsers != null)
{
loggedUsers.Add(new Logged()
{
FirstName = "John"
});
}
您將獲得更新。
一些事情:
1.您不需要為ObservableCollection<Logged> Logged
調用RaisePropertyChanged
public void Add(Logged user)
{
Logged.Add(user);
UpdateView(); //Check the List for users
//You dont need this RaisedPropertyChanged
RaisePropertyChanged("Logged"); //Notify View binding there is a change
}
2.Logged應該是一個屬性-ObservableCollection ObservableCollection<Logged> Logged {get; private set;}
ObservableCollection<Logged> Logged {get; private set;}
然后使用其構造函數對其進行初始化
public LoggedUsers
{
Logged = new ObservableCollection<Logged>();
}
3.為public class Logged
INotifyPropertyChanged接口,如果要在其構造函數之外更新值,則將需要它。
public string ClearPassword { get; set; }
應該
private string _clearPassword
publuc string ClearPassword
{
get { return _clearPassword;}
set
{
_clearPassword = value;
RaisePropertyChanged("ClearPassword");
}
}
對於其余的屬性也要這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.