dynamic order SecurityKey DataTemplate

This commit is contained in:
John Colvin committed 2019-10-01 17:08:32 -05:00
1 parent bd3ef33607
commit bed52db98a
5 files changed
+184 -1

No files matched your search

@@ -247,6 +247,31 @@
</TextBlock>
</DataTemplate>
</u:TemplateSelectorKeyValue>
<u:TemplateSelectorKeyValue Key="{x:Static u:TemplateKeys.SecurityKey}">
<DataTemplate>
<Grid>
<!--Note: Ideally, this 'securities BindingProxy' would be a static resource, but it only seems to detect the dynamic securities when it is in the visual tree.-->
<!--<Grid.Resources>
<u:BindingProxy x:Key="SecuritiesBindingProxy" x:Shared="False" Value="{DynamicResource {x:Static u:ResourceKeys.Securities}}"/>
</Grid.Resources>-->
<u:BindingProxy x:Name="SecuritiesBindingProxy" Value="{DynamicResource {x:Static u:ResourceKeys.Securities}}"/>
<TextBlock>
<TextBlock.Text>
<MultiBinding>
<MultiBinding.Converter>
<cv:MultiValueConverterPipe>
<cv:DictionaryLookupConverter/>
<x:Static MemberType="cv:ValueConverters" Member="SecuritySelectorText"/>
</cv:MultiValueConverterPipe>
</MultiBinding.Converter>
<Binding Path="Value" ElementName="SecuritiesBindingProxy"/>
<Binding Path="param.value"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</DataTemplate>
</u:TemplateSelectorKeyValue>
</usp:AlgoParamTemplateSelector.KeyedTemplates>
<!--Bool-->
<usp:AlgoParamTemplateSelector.BoolTemplate>
@@ -320,6 +345,21 @@
</usp:PriceEntryControl>
</DataTemplate>
</u:TemplateSelectorKeyValue>
<u:TemplateSelectorKeyValue Key="{x:Static u:TemplateKeys.SecurityKey}">
<DataTemplate>
<c:ContractPickerPopup
CloseOnCancel="True"
CloseOnOK="True"
Header="{Binding SelectedSecurity, RelativeSource={RelativeSource Self}, Converter={x:Static cv:ValueConverters.SecuritySelectorText}}"
IsEditMode="False"
IsLazy="True"
SecurityMap="{DynamicResource {x:Static u:ResourceKeys.Securities}}"
SelectedSecurityKey="{Binding param.value}"
ShowDialogSkirt="True"
OKClick="{x:Static c:ContractPickerPopup.FlushSelectedSecurityKeyHandler}"
/>
</DataTemplate>
</u:TemplateSelectorKeyValue>
</usp:AlgoParamTemplateSelector.KeyedTemplates>
<!--Bool-->
<usp:AlgoParamTemplateSelector.BoolTemplate>
@@ -1156,9 +1156,15 @@
<TextBlock VerticalAlignment="Center" Text="{Binding descriptor.ParamName, Converter={x:Static ut:DisplayNameHelper.SnakeToTitleCaseConverter}}"/>
<c:ContractPickerPopup
Grid.Column="2"
CloseOnCancel="True"
CloseOnOK="True"
Header="{Binding SelectedSecurity, RelativeSource={RelativeSource Self}, Converter={x:Static cv:ValueConverters.SecuritySelectorText}}"
IsEditMode="False"
IsEnabled="{Binding DataContext.editing_permissions, RelativeSource={RelativeSource AncestorType=ItemsControl}, Converter={StaticResource CanEditValuesConverter}}"
IsLazy="True"
SecurityMap="{DynamicResource ResourceKey={x:Static u:ResourceKeys.Securities}}"
SelectedSecurityKey="{Binding value, UpdateSourceTrigger=PropertyChanged}"
SelectedSecurityKey="{Binding value}"
ShowDialogSkirt="True"
/>
</Grid>
</DataTemplate>
@@ -28,9 +28,17 @@ namespace mbts.ui.converters {
IntTextConverter = new IntTextConverter(),
UIntTextConverter = new UIntTextConverter(),
QuantityMeasureTextConverter = ValueConverter.create<QuantityMeasureImmutable?, string>(fmt),
SecuritySelectorText,
SimpleString = new AnonymousValueConverter(obj => obj?.ToString()),
ThicknessToDouble = new ThicknessToDoubleConverter();
static ValueConverters() {
SecuritySelectorText = new NullSubstitutionConverterWrapper {
Converter = SimpleString,
ValueForNull = "(None)"
};
}
public static readonly IMultiValueConverter
AreEqualConverter = new AreEqualConverter();
@@ -0,0 +1,116 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
namespace mbts.ui.converters {
class DictionaryLookupConverter : IMultiValueConverter {
[ThreadStatic]
Dictionary<Type, Lookup> _type_impls_backing;
Dictionary<Type, Lookup> _type_impls => _type_impls_backing ?? (_type_impls_backing = new Dictionary<Type, Lookup>());
delegate bool Lookup(object source, object key, out object result);
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
if (values.Length == 2 && values[0] != null && values[1] != null) {
var lookup_type = values[0].GetType();
var lookup = _type_impls.GetOrAdd(lookup_type, TypeImplResolver.resolve);
if (lookup(values[0], values[1], out var result))
return result;
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) =>
throw new NotImplementedException();
static class TypeImplResolver {
public static Lookup resolve(Type lookup_type) =>
_prioritized_impls(lookup_type)
.OrderBy(tup => tup.priority)
.Select(tup => tup.impl)
.FirstOrDefault() ?? _lookup_fail;
static bool _lookup_fail(object source, object key, out object result) {
result = null;
return false;
}
static IEnumerable<(int priority, Lookup impl)> _prioritized_impls(Type lookup_type) {
foreach (var itype in lookup_type.GetInterfaces()) {
if (itype.IsGenericType) {
var gen_def = itype.GetGenericTypeDefinition();
if (gen_def == typeof(IDictionary<,>))
yield return (1, _get_generic_impl(nameof(_GenericImpls<int, int>.idictionary), itype));
if (gen_def == typeof(IReadOnlyDictionary<,>))
yield return (2, _get_generic_impl(nameof(_GenericImpls<int, int>.ireadonly_dictionary), itype));
if (gen_def == typeof(ILookup<,>))
yield return (3, _get_generic_impl(nameof(_GenericImpls<int, int>.ilookup), itype));
}
if (itype == typeof(IDictionary))
yield return (4, _NonGenericImpls.idictionary);
}
}
static Lookup _get_generic_impl(string impl_name, Type impl_type) =>
_get_generic_impl(impl_name, impl_type.GetGenericArguments());
static Lookup _get_generic_impl(string impl_name, Type[] key_value_types) =>
(Lookup)typeof(_GenericImpls<,>)
.MakeGenericType(key_value_types)
.GetField(nameof(_GenericImpls<int, int>.idictionary))
.GetValue(null);
static class _GenericImpls<TKey, TValue> {
public static readonly Lookup
idictionary = _idictionary,
ireadonly_dictionary = _ireadonly_dictionary,
ilookup = _ilookup;
static bool _idictionary(object lookup, object key, out object result) {
if (key is TKey k && lookup is IDictionary<TKey, TValue> d && d.TryGetValue(k, out var value)) {
result = value;
return true;
}
result = null;
return false;
}
static bool _ireadonly_dictionary(object lookup, object key, out object result) {
if (key is TKey k && lookup is IReadOnlyDictionary<TKey, TValue> rod && rod.TryGetValue(k, out var value)) {
result = value;
return true;
}
result = null;
return false;
}
static bool _ilookup(object lookup, object key, out object result) {
if ((key is TKey k) && lookup is ILookup<TKey, TValue> l && l.Contains(k)) {
result = l[k];
return true;
}
result = null;
return false;
}
}
static class _NonGenericImpls {
public static readonly Lookup
idictionary = _idictionary;
static bool _idictionary(object lookup, object key, out object result) {
var dict = (IDictionary)lookup;
if (dict.Contains(key)) {
result = dict[key];
return true;
}
result = null;
return false;
}
}
}
}
}
@@ -0,0 +1,13 @@
using System.Windows;
namespace mbts.ui {
class BindingProxy : FrameworkElement {
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(nameof(Value), typeof(object), typeof(BindingProxy));
public object Value {
get => GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
}
}