Manual binding to a collection element does not work
Posted: 03 Jan 2020, 11:11
I'm trying to manually re-direct bindings of a custom TextBlock control. The code below works perfectly in WPF and the first manual binding of the FontFamily also works in Noesis. The other bindings try to bind to an item in a collection. I've tried two ways: binding to the collection's item by setting the string PropertyPath and by setting the item in the binding's Source property. Both of them fail. Moreover, whenever I try to inspect the value of 'myBinding' during debugging the Unity editor crashes. Is there a different way to setup such binding in Noesis or is this a bug?
Code: Select all
private void BindSettings() {
// Font.
Bind("FontFamily", FontFamilyProperty);
if (!AreTextStyleSettingsAvailable(out int settingsIndex)) {
return;
}
#if !NOESIS
// TextSettings.
Bind($"TextStyles[{settingsIndex}].FontSize", FontSizeProperty);
Bind($"TextStyles[{settingsIndex}].FontWeight", FontWeightProperty, new ValueConverters.FontWeightConverter());
Bind($"TextStyles[{settingsIndex}].FontStyle", FontStyleProperty, new ValueConverters.FontStyleConverter());
Bind($"TextStyles[{settingsIndex}].TextDecoration", TextDecorationsProperty, new ValueConverters.TextDecorationConverter());
#else
// TextSettings.
Bind("FontSize", settingsIndex, FontSizeProperty);
Bind("FontWeight", settingsIndex, FontWeightProperty, new ValueConverters.FontWeightConverter());
Bind("FontStyle", settingsIndex, FontStyleProperty, new ValueConverters.FontStyleConverter());
Bind("TextDecoration", settingsIndex, TextDecorationsProperty, new ValueConverters.TextDecorationConverter());
#endif
}
private void Bind(string propName, DependencyProperty dp, IValueConverter valueConverter = null) {
Binding myBinding = new Binding {
Source = TextSettings,
Path = new PropertyPath(propName),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Converter = valueConverter
};
BindingOperations.SetBinding(this, dp, myBinding);
}
private void Bind(string propName, int index, DependencyProperty dp, IValueConverter valueConverter = null) {
Binding myBinding = new Binding {
Source = TextSettings.TextStyles[index],
Path = new PropertyPath(propName),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Converter = valueConverter
};
BindingOperations.SetBinding(this, dp, myBinding);
}