[英]Binding observable collection to listview wpf
我正在為工作的電子郵件客戶端,但是我無法將可觀察的集合以正確的方式綁定到我的列表視圖。 請提出建議,因為我無法在線找到確切的問題
C#
public partial class Mail : UserControl
{
ObservableCollection<GetMails> BindMe = new ObservableCollection<GetMails>();
public Mail()
{
InitializeComponent();
}
private void Grid_Loaded_1(object sender, RoutedEventArgs e)
{
BindMe = FetchAllHeaders(server, port, false, username, password);
lstMail.ItemsSource = BindMe.ToList();
}
private ObservableCollection<GetMails> FetchAllHeaders(string hostname, int port, bool useSsl, string username, string password)
{
// The client disconnects from the server when being disposed
using (Pop3Client client = new Pop3Client())
{
GetMails gm = new GetMails();
// Connect to the server
client.Connect(hostname, port, useSsl);
// Authenticate ourselves towards the server
client.Authenticate(username, password);
// Get the number of messages in the inbox
int messageCount = client.GetMessageCount();
// We want to download all messages
ObservableCollection<GetMails> getHeaders = new ObservableCollection<GetMails>();
// Messages are numbered in the interval: [1, messageCount]
// Ergo: message numbers are 1-based.
// Most servers give the latest message the highest number
for (int i = messageCount; i > 0; i--)
{
MessageHeader headers = client.GetMessageHeaders(i);
RfcMailAddress from = headers.From;
Message message = client.GetMessage(i);
gm.Header = client.GetMessageHeaders(i).Subject;
gm.From = from.MailAddress.ToString();
int count = 0;
foreach (MessagePart attachment in message.FindAllAttachments())
{
count++;
}
gm.NumberAttach = count;
getHeaders.Add(gm);
}
// Now return the fetched messages
return getHeaders;
}
}
}
XAML
<UserControl x:Class="VeriMail.Mail"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Loaded="Grid_Loaded_1" Margin="0,0,-132,0">
<ListView x:Name="lstMail" Height="399" VerticalAlignment="Top" Margin="0,0,-67,-99" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True">
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Subject" />
<GridViewColumn Width="140" Header="From" />
<GridViewColumn Width="140" Header="Number of Attachments"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
當我這樣做時得到的輸出就是VeriMail.GetMail
如果我將gridview更改為此:
<GridViewColumn Width="140" Header="Subject" DisplayMemberBinding="{Binding Header}"/>
<GridViewColumn Width="140" Header="From" DisplayMemberBinding="{Binding From}"/>
<GridViewColumn Width="140" Header="Number of Attachments" DisplayMemberBinding="{Binding NumberAttach}"/>
然后,輸出更改為我想要的信息,但它將復制一封電子郵件,最后一封電子郵件插入可觀察的集合中。
UPDATE :問題是當您調用ToList
,集合被復制,並且將來不會反映更改。 刪除該方法調用,它應該可以工作。
但是,最好在XAML中綁定ItemsSource
而不是在后面的代碼中進行綁定:
<ListView x:Name="lstMail" ItemsSource="{Binding BindMe, RelativeSource={RelativeSource AncestorType=UserControl}}" Height="399" VerticalAlignment="Top" Margin="0,0,-67,-99" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True">
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Subject" />
<GridViewColumn Width="140" Header="From" />
<GridViewColumn Width="140" Header="Number of Attachments"/>
</GridView>
</ListView.View>
</ListView>
並且也不要在FetchAllHeaders()
更改BindMe
集合引用本身,而是修改其內容。 否則,綁定將不起作用,因為再次更改了引用。
如另一個答案中所述,綁定到XAML是一種通常的做法(例如,使用MVVM模式)。 如果這樣做,您可能已經找到了該錯誤...
lstMail.ItemsSource = BindMe.ToList();
調用ToList()
將創建一個全新的List<GetMails>
其中包含可觀察集合的內容。 當您添加新項目時,列表不會更改-它是副本,對原始集合一無所知。
嘗試
lstMail.ItemsSource = BindMe;
這應該可以解決您的問題。
lstMail.ItemsSource = BindMe.ToList();
應該
lstMail.ItemsSource = BindMe;
綁定到列表不同於綁定到ObservableCollection。 可觀察集合是用於綁定的對象,因為它實現了ICollectionChanged。 清單沒有。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.