using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text;
using System.Windows.Data;
namespace Photoviewer.Common
{
///
/// Specialized converter to convert between a tag collection
/// and a string representation of the tags
///
public class TagsValueConverter : IValueConverter
{
#region IValueConverter Members
///
/// Converts a collection of tags to a single string representation
/// containing the tags separated by a comma
///
///
///
///
///
///
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is Collection))
{
return null;
}
StringBuilder outputBuilder = new StringBuilder();
bool first = true;
foreach (string item in (Collection)value)
{
if (!first)
{
outputBuilder.Append(", ");
}
outputBuilder.Append(item);
first = false;
}
return outputBuilder.ToString();
}
///
/// Converts a set of tags separated by a comma back to
/// a collection of tags
///
///
///
///
///
///
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string[] parts = value.ToString().Split(',');
Collection tags = new Collection();
foreach (string tag in parts)
{
tags.Add(tag.Trim());
}
return tags;
}
#endregion
}
}