namespace GameLibrary.Framework{
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using Caliburn.PresentationFramework;
using Caliburn.PresentationFramework.Actions;
using Caliburn.PresentationFramework.ApplicationModel;
using Microsoft.Practices.ServiceLocation;
public class ConventionBinder : DefaultBinder
{
private static readonly BooleanToVisibilityConverter _booleanToVisibilityConverter = new BooleanToVisibilityConverter();
private static readonly Dictionary<Type, DependencyProperty> _boundProperties =
new Dictionary<Type, DependencyProperty>
{
{ typeof(TextBox), TextBox.TextProperty },
{ typeof(ContentControl), View.ModelProperty },
{ typeof(TransitioningContentControl), View.ModelProperty },
{ typeof(ItemsControl), ItemsControl.ItemsSourceProperty },
{ typeof(TextBlock), TextBlock.TextProperty },
{ typeof(BusyIndicator), BusyIndicator.IsBusyProperty },
{ typeof(Rating), Rating.ValueProperty },
{ typeof(Border), Border.VisibilityProperty }
};
private static readonly DataTemplate _defaultTemplate = (DataTemplate)XamlReader.Load(
"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
"xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' " +
"xmlns:vm='clr-namespace:Caliburn.PresentationFramework.ApplicationModel;assembly=Caliburn.PresentationFramework'> " +
"<ContentControl vm:View.Model=\"{Binding}\" />" +
"</DataTemplate>"
);
public ConventionBinder(IServiceLocator serviceLocator, IActionFactory actionFactory, IMessageBinder messageBinder)
: base(serviceLocator, actionFactory, messageBinder)
{
}
protected override void ApplyBindingConventions(DependencyObject element, object model)
{
base.ApplyBindingConventions(element, model);
if (!(element is FrameworkElement))
return;
var viewType = model.GetType();
var properties = viewType.GetProperties();
foreach (var property in properties)
{
var foundControl = element.FindName(property.Name);
if (foundControl == null)
continue;
DependencyProperty boundProperty;
if (!_boundProperties.TryGetValue(foundControl.GetType(), out boundProperty))
continue;
if (((FrameworkElement)foundControl).GetBindingExpression(boundProperty) != null)
continue;
var binding = new Binding(property.Name)
{
Mode = property.CanWrite ? BindingMode.TwoWay : BindingMode.OneWay,
//Only available in SL4
//ValidatesOnDataErrors = Attribute.GetCustomAttributes(property, typeof(ValidationAttribute), true).Any()
};
if (boundProperty == UIElement.VisibilityProperty && typeof(bool).IsAssignableFrom(property.PropertyType))
binding.Converter = _booleanToVisibilityConverter;
//Only available in SL4
//else if (typeof(DateTime).IsAssignableFrom(property.PropertyType))
// binding.StringFormat = "{0:MM/dd/yyyy}";
BindingOperations.SetBinding(foundControl, boundProperty, binding);
var textBox = foundControl as TextBox;
if (textBox != null && boundProperty == TextBox.TextProperty)
{
textBox.TextChanged += delegate { textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource(); };
continue;
}
var itemsControl = foundControl as ItemsControl;
if (itemsControl != null && string.IsNullOrEmpty(itemsControl.DisplayMemberPath) && itemsControl.ItemTemplate == null)
{
itemsControl.ItemTemplate = _defaultTemplate;
continue;
}
}
}
}
}
|