public static ColumnInfo[] GetColumnInfos(IEnumerable <Type> types) { List <ColumnInfo> l = new List <ColumnInfo>(); Dictionary <string, ColumnInfo> dict = new Dictionary <string, ColumnInfo>(); foreach (Type t in types) { foreach (PropertyInfo prop in t.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { InputFieldAttribute attr = prop.GetCustomAttribute(typeof(InputFieldAttribute)) as InputFieldAttribute; if (attr == null) { continue; } if (attr.HiddenField) { // パスワード項目は除外 continue; } ColumnInfo info = new ColumnInfo(prop, attr); if (dict.ContainsKey(info.PropertyName)) { continue; } l.Add(info); dict.Add(info.PropertyName, info); } } l.Sort(); return(l.ToArray()); }
private static int ComparePropertyByInputFieldAttr(PropertyInfo item1, PropertyInfo item2) { if (item1 == null || item2 == null) { return((item1 == null ? 1 : 0) - (item2 == null ? 1 : 0)); } InputFieldAttribute attr1 = (InputFieldAttribute)item1.GetCustomAttribute(typeof(InputFieldAttribute)); InputFieldAttribute attr2 = (InputFieldAttribute)item2.GetCustomAttribute(typeof(InputFieldAttribute)); if (attr1 == null || attr2 == null) { return((attr1 == null ? 1 : 0) - (attr2 == null ? 1 : 0)); } return(attr1.Order.CompareTo(attr2.Order)); }
private void OnTargetPropertyChanged(DependencyPropertyChangedEventArgs e) { if (e.NewValue == e.OldValue) { return; } List <PropertyInfo> props = new List <PropertyInfo>(); foreach (PropertyInfo pi in Target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { InputFieldAttribute attr = (InputFieldAttribute)pi.GetCustomAttribute(typeof(InputFieldAttribute)); if (attr != null) { props.Add(pi); } } props.Sort(ComparePropertyByInputFieldAttr); GridProperties.RowDefinitions.Clear(); GridProperties.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); GridProperties.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); int r = 2; for (int i = 0; i < props.Count; i++, r++) { GridProperties.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); PropertyInfo prop = props[i]; InputFieldAttribute attr = (InputFieldAttribute)prop.GetCustomAttribute(typeof(InputFieldAttribute)); TextBlock lbl = new TextBlock(); lbl.Margin = new Thickness(2.0); lbl.HorizontalAlignment = HorizontalAlignment.Right; lbl.Text = attr?.Title; Grid.SetRow(lbl, r); GridProperties.Children.Add(lbl); string newName = "textBox" + prop.Name; TextBox tb = FindName(newName) as TextBox; if (tb == null) { tb = new TextBox(); tb.Name = newName; tb.Margin = new Thickness(2.0); Binding b1 = new Binding("Target." + prop.Name) { ElementName = "window", Mode = BindingMode.TwoWay }; tb.SetBinding(TextBox.TextProperty, b1); tb.TextChanged += TextBox_TextChanged; tb.LostFocus += TextBox_LostFocus; RegisterName(tb.Name, tb); } else { GridProperties.Children.Remove(tb); } Grid.SetColumn(tb, 1); Grid.SetRow(tb, r); GridProperties.Children.Add(tb); if (attr.HiddenField) { newName = "passwordBox" + prop.Name; PasswordBox pb = FindName(newName) as PasswordBox; if (pb == null) { pb = new PasswordBox(); pb.Name = newName; pb.Margin = new Thickness(2.0); //Binding b2 = new Binding("Text"); //b2.ElementName = tb.Name; //b2.Mode = BindingMode.TwoWay; //pb.SetBinding(PasswordBox.TextProperty, b2); pb.Password = Target.Password; pb.PasswordChanged += PbPassword_PasswordChanged; pb.Tag = tb; tb.TextChanged += TbPassword_TextChanged; tb.IsVisibleChanged += TbPassword_IsVisibleChanged; tb.Tag = pb; RegisterName(pb.Name, pb); } else { GridProperties.Children.Remove(pb); } Grid.SetColumn(pb, 1); Grid.SetRow(pb, r); GridProperties.Children.Add(pb); GridProperties.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); r++; newName = "checkBox" + prop.Name; CheckBox cb = FindName(newName) as CheckBox; if (cb == null) { cb = new CheckBox() { Name = newName, Content = "パスワードを表示", IsChecked = false }; RegisterName(cb.Name, cb); Binding b3 = new Binding("IsChecked") { ElementName = cb.Name, Mode = BindingMode.OneWay, Converter = new BooleanToVisibilityConverter() }; tb.SetBinding(VisibilityProperty, b3); Binding b4 = new Binding("IsChecked") { ElementName = cb.Name, Mode = BindingMode.OneWay, Converter = new InvertBooleanToVisibilityConverter() }; pb.SetBinding(VisibilityProperty, b4); Binding b5 = new Binding("IsEnabled") { RelativeSource = RelativeSource.Self, Mode = BindingMode.OneWay, Converter = new IsEnabledToColorConverter() }; cb.SetBinding(ForegroundProperty, b5); } else { GridProperties.Children.Remove(cb); } Grid.SetColumn(cb, 1); Grid.SetRow(cb, r); if (Target.IsPasswordHidden) { cb.IsEnabled = false; cb.IsChecked = false; } else { cb.IsEnabled = true; } GridProperties.Children.Add(cb); } } GridProperties.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); Grid.SetRow(textBlockTitleColor, r); Grid.SetRow(stackPanelTitleColor, r); UpdateTitleColor(); StackPanelMain.UpdateLayout(); }
internal ColumnInfo(PropertyInfo info, InputFieldAttribute attribute) { PropertyName = info.Name; Order = attribute.Order; HeaderText = attribute.Title; }