|
/*********************************************************************
* A LINQ Tutorial: WPF Data Binding with LINQ to SQL
* By: Abby Fichtner, http://www.TheHackerChickBlog.com
* Article URL: http://www.codeproject.com/KB/linq/linqtutorial3.aspx
* Licensed under The Code Project Open License (CPOL)
*********************************************************************/
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace LINQDemo.View
{
public partial class AddAuthors : UserControl
{
private readonly CollectionViewSource availableAuthors;
private Book book;
private EditDetails parentForm;
public AddAuthors( ) {
InitializeComponent( );
availableAuthors = (CollectionViewSource)FindResource( "availableAuthors" );
}
public void Display( EditDetails parentForm, Book book ) {
this.parentForm = parentForm;
this.book = book;
availableAuthors.Source = GetAvailableAuthors( );
Visibility = Visibility.Visible;
}
private ObservableCollection<Author> GetAvailableAuthors( ) {
var availableAuthors = new ObservableCollection<Author>( );
foreach( var author in ( from au in parentForm.BookCatalog.Authors orderby au.Name select au ) ) {
if( !book.Authors.Contains( author ) ) {
availableAuthors.Add( author );
}
}
return availableAuthors;
}
public void Hide( ){
Visibility = Visibility.Collapsed;
availableAuthors.ClearValue( CollectionViewSource.SourceProperty );
}
public void AddAuthor( object sender, RoutedEventArgs e ) {
Author author = ( sender as Button ).CommandParameter as Author;
if( author != null ) {
book.Authors.Add( author );
( (ObservableCollection<Author>)availableAuthors.Source ).Remove( author );
}
}
public void RemoveAuthor( Author author ) {
if( author != null ) {
book.Authors.Remove( author );
// Don't re-add the Author to available authors
// because if you delete & then re-add a M:M join relationship within the same
// "transaction" you'll get a DuplicateKeyException
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Abby Fichtner is a Microsoft Developer Evangelist and author of
The Hacker Chick Blog.
She's been developing custom software applications, wearing every hat imaginable, since 1994. Although, technically, she got her start at the age of 8 when her father brought home an Atari 800. In the evenings, they would sit together and type in the machine code from the Atari magazines – because that was the way serious geeks got their computer games!
Today, she works for Microsoft as a Developer Evangelist to the startup community - helping them to create the next generation of software.