WPF更新列表框数据绑定

时间:2014-06-16 19:33:41

标签: c# wpf xaml data-binding listbox

我是WPF的新手,正在从xml文件中对数据绑定装箱进行数据绑定,一切都在程序启动时正确加载,但是在插入新记录后我无法更新列表框。我读过的所有东西都指向使用我的ObservableCollection,但我无法弄清楚如何让Listbox刷新。我试过调用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>

1 个答案:

答案 0 :(得分:0)

根据ObservableCollection的源代码,我可以告诉您的问题很可能是您用来将 ContactList 对象添加到 ObservableCollection <的Add方法/ strong>是 ObservableCollection 继承自的集合类的一部分。这不会触发 ObservableCollection 上的 CollectionChanged 事件,因此永远不会通知您的绑定集合已更改。将每个项目添加到集合后,尝试调用 OnCollectionChanged 受保护的方法。