Replies: 2 comments
-
重新写一个编辑器,本人已解决using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;
using System.Reflection;
namespace Models.Editor
{
public class EnumPropertyEditor : PropertyEditorBase
{
// 重写对应的控件构建类,用于返回UI需要显示的控件实例
public override FrameworkElement CreateElement(PropertyItem propertyItem)
{
// 初始化 propertyItem.Value,防止为 null
if (propertyItem.Value == null)
{
throw new ArgumentException("PropertyItem.Value cannot be null.");
}
PropertyInfo property = (propertyItem.Value as SystemConfigPropertyGridDemoModel).GetType().GetProperty(propertyItem.PropertyName);
var range = property.GetValue(propertyItem.Value as SystemConfigPropertyGridDemoModel);
var enumType = range.GetType();
// 创建 ComboBox
var comboBox = new System.Windows.Controls.ComboBox
{
Margin = new Thickness(2),
VerticalAlignment = VerticalAlignment.Center
};
// 将枚举值添加到 ComboBox
foreach (var enumValue in Status.GetValues(enumType))
{
var enumMember = enumType.GetMember(enumValue.ToString()).FirstOrDefault();
var description = enumMember?.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false)
.Cast<System.ComponentModel.DescriptionAttribute>()
.FirstOrDefault()?.Description ?? enumValue.ToString();
comboBox.Items.Add(new ComboBoxItem
{
Content = description,
Tag = enumValue
});
}
// 设置 ComboBox 的选中值
comboBox.SelectedValuePath = "Tag";
comboBox.SelectedItem = comboBox.Items
.Cast<ComboBoxItem>()
.FirstOrDefault(item => item.Tag.Equals(propertyItem.Value));
// 绑定选中的值到属性
var binding = new Binding("SelectedValue")
{
Source = comboBox,
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
comboBox.SetBinding(System.Windows.Controls.ComboBox.SelectedValueProperty, binding);
return comboBox;
}
// 设置对应实体属性与控件关联的依赖属性
public override DependencyProperty GetDependencyProperty()
{
// 返回枚举属性的 DependencyProperty,表示默认绑定属性
return System.Windows.Controls.ComboBox.SelectedValueProperty;
}
}
} public enum Status
{
[Description("正在处理")]
Processing,
[Description("已完成")]
Completed,
[Description("已取消")]
Canceled
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
我之前也是重写了一个
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
这样无法显示出中文,只能显示出
Beta Was this translation helpful? Give feedback.
All reactions