Hi, I'm using Linq to XML with databinding, and it is partly working in that my textbox fields correctly display the selected item from my ListBox. The Listbox is databound to a List<> which is loaded from an XML file at initialisation time, and the textbox's are also databound to the same List<>.
I've also written a function that clears and enables the textbox's, allows the user to type in there and then click a button to add the new item.
When the add button is clicked, the new item is correctly added to the dataset List<> (I can see the number of items grow, and can display new item data on a label), however the ListBox does not reflect the newly added item.
I followed an example which I got from Codeproject, but that project didn't go far enough so I am trying to expand it.
My code is included below, if anyone can point out what I am missing or doing wrong, or even suggest a better way.
BTW, all this takes place inside a VSTO Outlook addin, and the windows are WPF, not forms.
Thanks, will.
The first bit - the XAML for my listbox
<ListView HorizontalAlignment="Left" Margin="45,10,0,82" Name="jobListView" Width="316" DataContext="{Binding}" ItemsSource="{Binding}" SelectionChanged="lbFolderList_SelectionChanged">
<ListView.View>
<GridView >
<GridView.Columns>
<GridViewColumn Width="90" Header="Created" DisplayMemberBinding="{Binding Path=created}" />
<GridViewColumn Width="250" Header="Outlook Folder" DisplayMemberBinding="{Binding Path=olfldr}" />
<GridViewColumn Width="250" Header="Msg Folder" DisplayMemberBinding="{Binding Path=msgfldr}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
The code for one of the textbox's used for viewing/adding
<TextBox Name="oLFolderEdit" Height="27" Margin="405,82,370,0" VerticalAlignment="Top" Text="{Binding Path=olfldr}" Focusable="True" IsEnabled="False" />
The .cs file for the above XAML (not all fields and code for them is inluded, they all work pretty much the same)
namespace SVPUtilities
{
/// <summary>
/// Interaction logic for EmailMonitor.xaml
/// </summary>
public partial class EmailMonitor
{
List<cJobMon> jobList = new List<cJobMon>();
public EmailMonitor()
{
InitializeComponent();
jobList = cDAL_JobMon.LoadJOBMON();
jobListView.DataContext = jobList;
}
private void lbFolderList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
oLFolderEdit.Text = jobList[jobListView.SelectedIndex].olfldr;
}
private void AddFolder_Click(object sender, RoutedEventArgs e)
{
oLFolderEdit.IsEnabled = true;
oLFolderEdit.Text = "";
jobListView.IsEnabled = false;
}
private void FolderOk_Click(object sender, RoutedEventArgs e)
{
jobList.Add(new cJobMon()
{
olfldr=oLFolderEdit.Text,
});
FolderListLabel.Content = "Folder List (" + jobList.Count + ")";
}
}
}
and finally the class used to populate the listbox on startup (all fields inluded in the class here that were omitted in the code above)
namespace SVPUtilities.DAL
{
public class cJobMon
{
public string created { get; set; }
public string olfldr { get; set; }
public string msgfldr { get; set; }
public string sphrs { get; set; }
}
class cDAL_JobMon
{
public static List<cJobMon> LoadJOBMON()
{
List<cJobMon> ListJobRecords = new List<cJobMon>();
int i = 0;
// Execute the query using the LINQ to XML
var jobs = from j in XElement.Load(@"C:\devel\VS\SV Utilities\SVPUtilities\job_folders.xml").Elements("job") select j;
foreach (var job in jobs)
{
i++;
cJobMon lJob = new cJobMon
{
created = job.Element("created").Value,
olfldr = job.Element("olfldr").Value,
msgfldr = job.Element("msgfldr").Value,
sphrs = job.Element("sphrs").Value,
};
ListJobRecords.Add(lJob);
}
return ListJobRecords;
}
}
}