1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.Internal.PropertyEditing.Editors
7 using System.Windows.Controls;
8 using System.Windows.Data;
9 using System.Globalization;
11 using System.Windows.Automation;
13 sealed class FlagPanel : StackPanel
15 public static readonly DependencyProperty FlagStringProperty =
16 DependencyProperty.Register("FlagString", typeof(string), typeof(FlagPanel), new PropertyMetadata(string.Empty));
18 public static readonly DependencyProperty FlagTypeProperty = DependencyProperty.Register(
22 new PropertyMetadata(null, new PropertyChangedCallback(OnFlagTypeChanged)));
26 get { return (Type)GetValue(FlagTypeProperty); }
27 set { SetValue(FlagTypeProperty, value); }
30 public string FlagString
32 get { return (string)GetValue(FlagStringProperty); }
33 set { SetValue(FlagStringProperty, value); }
36 static void OnFlagTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
38 Type flagType = args.NewValue as Type;
39 Fx.Assert(flagType == null || flagType.IsEnum, "FlagType should be null or enum");
40 Fx.Assert(flagType == null || Attribute.IsDefined(flagType, typeof(FlagsAttribute)), "FlagType should be null or have flags attribute");
48 FlagPanel panel = sender as FlagPanel;
49 string[] flagNames = flagType.GetEnumNames();
50 string zeroValueString = Enum.ToObject(flagType, 0).ToString();
51 foreach (string flagName in flagNames)
53 if (zeroValueString.Equals("0") || !flagName.Equals(zeroValueString))
55 CheckBox checkBox = new CheckBox();
56 panel.Children.Add(checkBox);
57 checkBox.Content = flagName;
58 checkBox.DataContext = panel;
59 checkBox.SetValue(AutomationProperties.AutomationIdProperty, flagName);
60 Binding binding = new Binding("FlagString");
61 binding.Mode = BindingMode.TwoWay;
62 binding.Converter = new CheckBoxStringConverter(index);
63 binding.ConverterParameter = panel;
64 checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
70 sealed class CheckBoxStringConverter : IValueConverter
74 public CheckBoxStringConverter(int index)
79 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
81 string str = (value as string).ToUpperInvariant();
82 FlagPanel panel = parameter as FlagPanel;
83 if (str.Contains((panel.Children[this.index] as CheckBox).Content.ToString().ToUpperInvariant()))
93 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
95 FlagPanel panel = parameter as FlagPanel;
96 string str = string.Empty;
97 for (int i = 0; i < panel.Children.Count; i++)
99 if ((i != this.index && ((bool)(panel.Children[i] as CheckBox).IsChecked)) ||
100 (i == this.index && (bool)value))
102 if (!string.IsNullOrEmpty(str))
106 str += (panel.Children[i] as CheckBox).Content.ToString();
109 if (string.IsNullOrEmpty(str))
111 Type flagType = panel.FlagType;
112 Fx.Assert(flagType != null && flagType.IsEnum, "FlagType should be enum");
113 Fx.Assert(Attribute.IsDefined(flagType, typeof(FlagsAttribute)), "FlagType should have flags attribute");
115 return Enum.ToObject(flagType, 0).ToString();