作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我通过以下方式在我的 UI 中创建了一些项目
Buttons.cs
[Browsable(true)]
[Display(Order = 1, Name = "Object1", GroupName = "Objects", ResourceType typeof(Resources.DisplayNames) , AutoGenerateField =false)]
public ButtonViewModel Button1{ get; set; }
[Browsable(true)]
[Display(Order = 2, Name = "Object2", GroupName = "Objects", ResourceType typeof(Resources.DisplayNames) , AutoGenerateField =false)]
public ButtonViewModel Button2{ get; set; }
//etc
我想做的是根据某些条件显示或隐藏这些 UI 元素。我看到如果我将 AutoGenerateField 设置为 true/false,我会得到想要的结果,有没有办法在运行时设置该值 true/false? (如果可能的话)
有没有其他方法可以同时做到这一点?比如每次添加/删除显示属性。
编辑
PropertyGridView.xaml
<Grid>
<telerik:RadPropertyGrid telerik:StyleManager.Theme="Fluent"
x:Name="PropertyGrid"
IsGrouped="True"
Item="{Binding SelectedItem}"
PropertySetMode="Union"
RenderMode="Flat"
SortAndGroupButtonsVisibility="Collapsed">
</telerik:RadPropertyGrid>
</Grid>
PropertyGridViewModel.cs
public class PropertyGridViewModel : Screen, IPropertyGridViewModel, IHandle<ItemSelectionMessage>
{
private IEventAggregator _eventAggregator;
private IItem _item;
public PropertyGridViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
protected override void OnActivate()
{
base.OnActivate();
_eventAggregator.Subscribe(this);
}
protected override void OnDeactivate(bool close)
{
base.OnDeactivate(close);
_eventAggregator.Unsubscribe(this);
}
public IItem SelectedItem
{
get
{
return _item;
}
set
{
_item = value;
NotifyOfPropertyChange(() => SelectedItem);
}
}
public void Handle(ItemSelectionMessage message)
{
SelectedItem = message.Item;
}
}
并且通过的项目是上面的 Buttons.cs。
最佳答案
您可以为每个可见性状态创建两个实现IItem
接口(interface)的类,并根据当前可见性状态将适当的一个设置为RadPropertyGrid
。但它只适用于数量不多的可见性状态(你必须为它们中的每一个创建单独的类)。另一种方法是为每个带有反射的属性动态设置 Display
属性。但我建议您遵循第一种拆分类的方法。
// additional methods for getting appropriate instance of your class
public static List<Type> GetInterfaceTypes<Interface>()
{
Type serachInterface = typeof(Interface);
List<Type> findClasses = serachInterface.Assembly.GetTypes().Where
(
t => t.IsClass && !t.IsAbstract &&
serachInterface.IsAssignableFrom(t)
).ToList();
return findClasses;
}
public static List<Interface> GetInstances<Interface>(params object[] paramArray)
{
List<Interface> returnInstances = new List<Interface>();
List<Type> foundTypes = GetInterfaceTypes<Interface>();
foundTypes.ForEach(x =>
{
returnInstances.Add((Interface)Activator.CreateInstance(x,args:paramArray));
});
return returnInstances;
}
// your handler from PropertyGridViewModel.cs
public IItem SelectedItem { get; private set; }
public void Handle(ItemSelectionMessage message)
{
IItem item = GetInstances<IItem>(_eventAggregator).FirstOrDefault(x => x.Visibility == message.Visibility);
SelectedItem = item;
}
public class ItemSelectionMessage
{
public VisibilityStates Visibility { set; get; }
}
// enum that is describe you visibility states
public enum VisibilityStates
{
Button1,
Button2
}
// interface and classes that are implemented visibility interface
public interface IItem
{
VisibilityStates Visibility { get; }
}
public class Button1StateClass : IItem
{
public VisibilityStates Visibility { get => VisibilityStates.Button1; }
[Browsable(true)]
[Display(Order = 1, Name = "Object1", GroupName = "Objects", ResourceType typeof(Resources.DisplayNames), AutoGenerateField = true)]
public ButtonViewModel Button1 { get; set; }
public Button1StateClass(IEventAggregator eventAggregator) : base(eventAggregator)
{
}
public Button1StateClass()
{
}
}
public class Button2StateClass : IItem
{
public VisibilityStates Visibility { get => VisibilityStates.Button2; }
[Browsable(true)]
[Display(Order = 2, Name = "Object2", GroupName = "Objects", ResourceType typeof(Resources.DisplayNames), AutoGenerateField = true)]
public ButtonViewModel Button2 { get; set; }
public Button2StateClass(IEventAggregator eventAggregator) : base(eventAggregator)
{
}
public Button2StateClass()
{
}
}
关于c# - 如何在 WPF 中以编程方式添加或删除 DisplayAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58711947/
我是一名优秀的程序员,十分优秀!