Created
July 6, 2014 16:53
-
-
Save tmyt/85b79ad573b758102f6e to your computer and use it in GitHub Desktop.
かずきさんがやってたのでやってみました
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Windows.UI.Xaml; | |
namespace XamlUtils.Common | |
{ | |
public class SelectorCondition | |
{ | |
public static object GetValue(DependencyObject obj) | |
{ | |
return obj.GetValue(ValueProperty); | |
} | |
public static void SetValue(DependencyObject obj, object value) | |
{ | |
obj.SetValue(ValueProperty, value); | |
} | |
public static readonly DependencyProperty ValueProperty = | |
DependencyProperty.RegisterAttached("Value", typeof(object), typeof(SelectorCondition), new PropertyMetadata(null)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using Microsoft.Xaml.Interactions.Core; | |
namespace XamlUtils.Common | |
{ | |
public class TypeConverter | |
{ | |
public static object ChangeType(Type targetType, object from) | |
{ | |
var typeInfo = targetType.GetTypeInfo(); | |
if (from == null) | |
return typeInfo.IsValueType ? Activator.CreateInstance(targetType) : null; | |
if (typeInfo.IsAssignableFrom(from.GetType().GetTypeInfo())) | |
return from; | |
// Convert | |
var str = from.ToString(); | |
return typeInfo.IsEnum ? Enum.Parse(targetType, str) : ConvertType(str, targetType.FullName); | |
} | |
private static object ConvertType(string from, string type) | |
{ | |
try | |
{ | |
// Get Internal method | |
var typeName = typeof(EventTriggerBehavior).AssemblyQualifiedName | |
.Replace(".EventTriggerBehavior,", ".TypeConverterHelper,"); | |
var typeConverterHelperType = Type.GetType(typeName); | |
var typeConverterHelperConvert = typeConverterHelperType | |
.GetRuntimeMethod("Convert", new[] { typeof(string), typeof(string) }); | |
return typeConverterHelperConvert.Invoke(null, new object[] { from, type }); | |
} | |
catch | |
{ | |
return null; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using Windows.UI.Xaml; | |
using Windows.UI.Xaml.Controls; | |
using Windows.UI.Xaml.Markup; | |
namespace XamlUtils.Common | |
{ | |
[ContentProperty(Name = "Styles")] | |
public class XamlStyleSelector : StyleSelector | |
{ | |
public List<Style> Styles { get; set; } | |
public string Property { get; set; } | |
public XamlStyleSelector() | |
{ | |
Styles = new List<Style>(); | |
} | |
private bool Evalute(object source, object target) | |
{ | |
if (source == null || target == null) return false; | |
if (!(source is string)) | |
{ | |
var value = TypeConverter.ChangeType(source.GetType(), target); | |
return source.Equals(value); | |
} | |
return source.Equals(target) || source == target; | |
} | |
private Style GetDefault() | |
{ | |
return Styles.FirstOrDefault(style => SelectorCondition.GetValue(style) == null); | |
} | |
private Style Evalute(object value) | |
{ | |
return Styles.FirstOrDefault(style => Evalute(value, SelectorCondition.GetValue(style))) | |
?? GetDefault(); | |
} | |
protected Style SelectStyleCore(object item) | |
{ | |
var type = item.GetType(); | |
var value = (object)item.GetType().Name; | |
if (Property != null) | |
{ | |
var prop = type.GetRuntimeProperty(Property); | |
if (prop == null) return null; | |
value = prop.GetValue(item); | |
} | |
return Evalute(value); | |
} | |
protected override Style SelectStyleCore(object item, DependencyObject container) | |
{ | |
return SelectStyleCore(item); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using Windows.UI.Xaml; | |
using Windows.UI.Xaml.Controls; | |
using Windows.UI.Xaml.Markup; | |
namespace XamlUtils.Common | |
{ | |
[ContentProperty(Name = "Templates")] | |
public class XamlTemplateSelector : DataTemplateSelector | |
{ | |
public List<DataTemplate> Templates { get; set; } | |
public string Property { get; set; } | |
public XamlTemplateSelector() | |
{ | |
Templates = new List<DataTemplate>(); | |
} | |
private bool Evalute(object source, object target) | |
{ | |
if (source == null || target == null) return false; | |
if (!(source is string)) | |
{ | |
var value = TypeConverter.ChangeType(source.GetType(), target); | |
return source.Equals(value); | |
} | |
return source.Equals(target) || source == target; | |
} | |
private DataTemplate GetDefault() | |
{ | |
return Templates.FirstOrDefault(style => SelectorCondition.GetValue(style) == null); | |
} | |
private DataTemplate Evalute(object value) | |
{ | |
return Templates.FirstOrDefault(style => Evalute(value, SelectorCondition.GetValue(style))) | |
?? GetDefault(); | |
} | |
protected override DataTemplate SelectTemplateCore(object item) | |
{ | |
var type = item.GetType(); | |
var value = (object)item.GetType().Name; | |
if (Property != null) | |
{ | |
var prop = type.GetRuntimeProperty(Property); | |
if (prop == null) return null; | |
value = prop.GetValue(item); | |
} | |
return Evalute(value); | |
} | |
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) | |
{ | |
return SelectTemplateCore(item); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment