private void UpdateContent() { if (panel == null) return; panel.Children.Clear(); if (Value == null) return; var enumValues = Enum.GetValues(Value.GetType()).FilterOnBrowsableAttribute(); var converter = new EnumToBooleanConverter { EnumType = Value.GetType() }; var relativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(RadioButtonList), 1); var descriptionConverter = new EnumDescriptionConverter(); foreach (var itemValue in enumValues ) { var rb = new RadioButton { Content = descriptionConverter.Convert(itemValue, typeof(string), null, CultureInfo.CurrentCulture) }; // rb.IsChecked = Value.Equals(itemValue); var isCheckedBinding = new Binding("Value") { Converter = converter, ConverterParameter = itemValue, Mode = BindingMode.TwoWay, RelativeSource = relativeSource }; rb.SetBinding(ToggleButton.IsCheckedProperty, isCheckedBinding); var itemMarginBinding = new Binding("ItemMargin") { RelativeSource = relativeSource }; rb.SetBinding(MarginProperty, itemMarginBinding); panel.Children.Add(rb); } }
public void EnumToBooleanConverterConverts() { EnumToBooleanConverter converter = new EnumToBooleanConverter(); object value = converter.Convert(TransactionType.Buy, typeof(TransactionType), TransactionType.Buy, null); Assert.IsTrue((bool)value); }
public EnumToBooleanConverterTest() { Converter = new EnumToBooleanConverter() { EnumType = typeof(MyEnum) }; }
public void EnumToBooleanConverterConvertsBack() { EnumToBooleanConverter converter = new EnumToBooleanConverter(); object value = converter.ConvertBack(true, typeof(TransactionType), TransactionType.Sell, null); Assert.AreEqual(TransactionType.Sell, value); }
public void EnumToBooleanConverterConvertsIfValueIsNull() { EnumToBooleanConverter converter = new EnumToBooleanConverter(); object value = converter.Convert(null, typeof(TransactionType), TransactionType.Buy, null); Assert.IsFalse((bool)value); }
public void EnumToBooleanConverterConvertsBackIfValueIsFalse() { EnumToBooleanConverter converter = new EnumToBooleanConverter(); object value = converter.ConvertBack(false, typeof(TransactionType), TransactionType.Sell, null); Assert.IsNull(value); }
public void ConvertBooleanToEnum() { var converter = new EnumToBooleanConverter(); Assert.AreEqual(Binding.DoNothing, converter.ConvertBack("test", typeof(string), "test", new CultureInfo(1))); }
public void EnumToBooleanConverter() { var converter = new EnumToBooleanConverter(); Assert.AreEqual(true, converter.Convert(PasteBarPosition.Top, typeof(ConvertersTests), "Top", CultureInfo.CurrentCulture)); Assert.AreEqual(false, converter.Convert(PasteBarPosition.Bottom, typeof(ConvertersTests), "Top", CultureInfo.CurrentCulture)); }
public void EnumToBooleanConverterTest() { var converter = new EnumToBooleanConverter(); Assert.IsFalse((bool)converter.Convert(null, null, "foo", null)); Assert.IsFalse((bool)converter.Convert(AppBarClosedDisplayMode.Compact, null, null, null)); Assert.IsFalse((bool)converter.Convert("foo", null, "foo", null)); Assert.IsTrue((bool)converter.Convert(AppBarClosedDisplayMode.Compact, null, "Compact", null)); Assert.IsFalse((bool)converter.Convert(AppBarClosedDisplayMode.Compact, null, "Test", null)); Assert.AreEqual(AppBarClosedDisplayMode.Compact, converter.ConvertBack(true, typeof(AppBarClosedDisplayMode), "Compact", null)); }
/// <summary> /// Creates the label control. /// </summary> /// <param name="pi">The property item.</param> /// <returns> /// An element. /// </returns> private FrameworkElement CreateLabel(PropertyItem pi) { FrameworkElement propertyLabel = null; if (pi.IsOptional) { var cb = new CheckBox { Content = pi.DisplayName, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(5, 0, 0, 0) }; cb.SetBinding( ToggleButton.IsCheckedProperty, pi.OptionalDescriptor != null ? new Binding(pi.OptionalDescriptor.Name) : new Binding(pi.Descriptor.Name) { Converter = NullToBoolConverter }); var g = new Grid(); g.Children.Add(cb); propertyLabel = g; } if (pi.IsEnabledByRadioButton) { var rb = new RadioButton { Content = pi.DisplayName, GroupName = pi.RadioDescriptor.Name, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(5, 0, 0, 0) }; var converter = new EnumToBooleanConverter(); converter.EnumType = pi.RadioDescriptor.PropertyType; rb.SetBinding( RadioButton.IsCheckedProperty, new Binding(pi.RadioDescriptor.Name) { Converter = converter, ConverterParameter = pi.RadioValue }); var g = new Grid(); g.Children.Add(rb); propertyLabel = g; } if (propertyLabel == null) { propertyLabel = new Label { Content = pi.DisplayName, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(0, 4, 0, 0) }; } propertyLabel.Margin = new Thickness(0, 0, 4, 0); return propertyLabel; }
/// <summary> /// Updates the content. /// </summary> private void UpdateContent() { if (this.panel == null) { return; } this.panel.Children.Clear(); var enumType = this.EnumType; if (enumType != null) { var ult = Nullable.GetUnderlyingType(enumType); if (ult != null) { enumType = ult; } } if (this.Value != null) { enumType = this.Value.GetType(); } if (enumType == null || !typeof(Enum).IsAssignableFrom(enumType)) { return; } var enumValues = Enum.GetValues(enumType).FilterOnBrowsableAttribute(); var converter = new EnumToBooleanConverter { EnumType = enumType }; foreach (var itemValue in enumValues) { var rb = new RadioButton { Content = this.DescriptionConverter.Convert( itemValue, typeof(string), null, CultureInfo.CurrentCulture), Padding = this.ItemPadding }; var isCheckedBinding = new Binding("Value") { Converter = converter, ConverterParameter = itemValue, Source = this, Mode = BindingMode.TwoWay }; rb.SetBinding(ToggleButton.IsCheckedProperty, isCheckedBinding); rb.SetBinding(MarginProperty, new Binding("ItemMargin") { Source = this }); this.panel.Children.Add(rb); } }
public void ConvertEnumToBoolean() { var converter = new EnumToBooleanConverter(); Assert.AreEqual(true, converter.Convert("test", typeof(string), "test", new CultureInfo(1))); }
internal EnumValueCollection(EnumToBooleanConverter parent) { this.parent = parent; }