簡體   English   中英

WPF更新列表框數據綁定

[英]WPF Update Listbox Databinding

我是WPF的新手,正在處理從xml文件對列表框進行數據綁定,在程序啟動時一切都能正確加載,但是在插入新記錄后,無法進行列表框更新。 我讀過的所有內容都指向我使用的ObservableCollection,但是我不知道如何刷新列表框。 我嘗試過調用ItemsSource的更新,但是它似乎仍然無法正常工作。 理想情況下,我只希望有一個“刷新”按鈕,用戶可以單擊該按鈕來更新列表框。 是否有人對調用列表框進行更新有任何建議?

public class ContactList 
{
    string contactFile = @"U:\Peridot\Users\" + Program.getUser.ToString() + ".xml";

    public ContactList()
    {
    }

    public ContactList(string contactFullName, string contactCellNumber,string contactBusinessNumber, string contactExtension, string contactEmail, string contactStatus,string contactAuralinkStatus, string contactAuralinkID)
    {
        this.ContactFullName = contactFullName;
        this.ContactCellNumber = contactCellNumber;
        this.ContactBusinessNumber = contactBusinessNumber;
        this.ContactExtension = contactExtension;
        this.ContactEmail = contactEmail;
        this.ContactStatus = contactStatus;
        this.ContactAuralinkStatus = contactAuralinkStatus;
        this.ContactAuralinkID = contactAuralinkID;
    }



    private string ContactFullName;

    public string PropContactFullName
    {
        get { return ContactFullName; }
        set { ContactFullName = value; }
    }

    private string ContactCellNumber;

    public string PropContactCellNumber
    {
        get { return ContactCellNumber; }
        set { ContactCellNumber = value; }
    }

    private string ContactBusinessNumber;

    public string PropContactBusinessNumber
    {
        get { return ContactBusinessNumber; }
        set { ContactBusinessNumber = value; }
    }

    private string ContactEmail;

    public string PropContactEmail
    {
        get { return ContactEmail; }
        set { ContactEmail = value; }
    }

    private string ContactStatus;

    public string PropContactStatus
    {
        get { return ContactStatus; }
        set { ContactStatus = value; }
    }

    private string ContactAuralinkStatus;

    public string PropContactAuralinkStatus
    {
        get { return ContactAuralinkStatus; }
        set { ContactAuralinkStatus = value; }
    }


    public string ContactAuralinkID;

    public string PropContactAuralinkID
    {
        get { return ContactAuralinkID; }
        set { ContactAuralinkID = value; }
    }


    private string ContactExtension;

    public string PropContactExtension
    {
        get { return ContactExtension; }
        set { ContactExtension = value; }
    }


}

public class Contacts : System.Collections.ObjectModel.ObservableCollection<ContactList>
{
    string contactFile = @"U:\Peridot\Users\" + Program.getUser.ToString() + ".xml";

//Added this
    public event NotifyCollectionChangedEventHandler CollectionChanged;

    protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if (CollectionChanged != null)
        {
            CollectionChanged(this, e);
        }
    }

    public Contacts(): base()
    {

        getContactFile();

        XDocument doc = XDocument.Load(contactFile);
        var contacts = from r in doc.Descendants("Contact")
                       select new
                       {
                           FullName = r.Element("FullName").Value,
                           CellNumber = r.Element("CellNumber").Value,
                           BusinessNumber = r.Element("BusinessNumber").Value,
                           Extension = r.Element("Extension").Value,
                           Email = r.Element("Email").Value,
                           AuralinkID = r.Element("AuralinkID").Value
                       };
        foreach (var r in contacts)
        {
            Add(new ContactList(r.FullName,r.CellNumber , r.BusinessNumber,r.Extension, r.Email, "", "",r.AuralinkID));
        }  
    }


    private void getContactFile()
    {
        if (!File.Exists(contactFile))
        {
            new XDocument(
                new XElement("Contacts"
                )
            )
            .Save(contactFile);
        }
    }
}


private void addContactICON_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (!doesContactExist())
        {
            try
            {

                XDocument doc = XDocument.Load(@"U:\Peridot\Users\" + Program.getUser.ToString() + ".xml");
                XElement contact = new XElement("Contact");
                contact.Add(new XElement("ContactID", contactID.ToString()));
                contact.Add(new XElement("FullName", contactNameLBL.Content.ToString()));
                contact.Add(new XElement("CellNumber", c1.Content.ToString()));
                contact.Add(new XElement("BusinessNumber", businessPhoneIcon.ToolTip.ToString()));
                contact.Add(new XElement("Extension", c3.Content.ToString()));
                contact.Add(new XElement("Email", emailIcon.ToolTip.ToString()));
                contact.Add(new XElement("AuralinkID", videoIcon.ToolTip.ToString()));

                doc.Element("Contacts").Add(contact);
                doc.Save(@"U:\Peridot\Users\" + Program.getUser.ToString() + ".xml");
                MessageBox.Show(contactNameLBL.Content.ToString() + " has been added to your contacts.");


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        else
            MessageBox.Show("Contact Already Exists");
    }

XAML

<StackPanel>
    <StackPanel.Resources>
        <local:Contacts x:Key="contactListobj"></local:Contacts>
    </StackPanel.Resources>
    <ListBox x:Name="contactList" Width="305" Margin="5,3,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource contactListobj}}" Height="450" IsSynchronizedWithCurrentItem="True">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" >
                    <TextBlock Text="{Binding PropContactFullName}" ToolTip="{Binding PropContactFullName}" Height="35" Width="175" FontSize="12"/>
                    <TextBlock x:Name="contactEmailLBL" Text="{Binding PropContactEmail}" ToolTip="{Binding PropContactEmail}" Cursor="Hand" Width="30" Height="35" MouseLeftButtonUp="contactEmailLBL_MouseLeftButtonUp" Foreground="{x:Null}" FontSize="1">
                        <TextBlock.Background>
                            <ImageBrush Stretch="Uniform" ImageSource="Images/emailICON.png"/>
                        </TextBlock.Background>
                    </TextBlock>
                    <TextBlock x:Name="cellNumberLBL" Text="{Binding PropContactCellNumber}" ToolTip="{Binding PropContactCellNumber}" Cursor="Hand" MouseLeftButtonUp="cellNumberLBL_MouseLeftButtonUp" Width="30" Height="35" Foreground="{x:Null}" FontSize="1">
                        <TextBlock.Background>
                            <ImageBrush Stretch="Uniform" ImageSource="Images/mobilePhoneICON.png"/>
                        </TextBlock.Background>
                    </TextBlock>
                    <TextBlock x:Name="businessNumberLBL" Text="{Binding PropContactBusinessNumber}" ToolTip="{Binding PropContactBusinessNumber}" Cursor="Hand" Width="30" Height="35" MouseLeftButtonUp="businessNumberLBL_MouseLeftButtonUp" Foreground="{x:Null}" FontSize="1">
                        <TextBlock.Background>
                            <ImageBrush Stretch="Uniform" ImageSource="Images/BusinessPhoneICON.png"/>
                        </TextBlock.Background>
                    </TextBlock>
                    <TextBlock x:Name="auralinkLBL" Text="{Binding PropContactAuralinkID}" ToolTip="{Binding PropContactAuralinkID}" Cursor="Hand" Width="30" Height="35" Foreground="{x:Null}" FontSize="1" MouseLeftButtonUp="auralinkLBL_MouseLeftButtonUp">
                        <TextBlock.Background>
                            <ImageBrush Stretch="Uniform" ImageSource="Images/VideoICON.png"/>
                        </TextBlock.Background>
                    </TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

根據基於ObservableCollection的源代碼的判斷,問題很可能是您用來將ContactList對象添加到ObservableCollection的Add方法是ObservableCollection繼承的Collection類的一部分。 這不會在ObservableCollection上觸發CollectionChanged事件,因此永遠不會通知您的綁定該集合已更改。 將每個項目添加到集合后,嘗試調用OnCollectionChanged受保護的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM