简体   繁体   English

Xceed PropertyGrid 中的条件隐藏/显示

[英]Conditional Hide/Show in Xceed PropertyGrid

I'm using the Xceed Property Grid for my WPF application.我正在为我的 WPF 应用程序使用 Xceed 属性网格。 I have a situation where a property field renders other attributes obsolete based on the selection.我有一种情况,属性字段根据选择使其他属性过时。 However the only thing I can do currently is show everything and let the user figure out what should be filled in and what should not.然而,我目前唯一能做的就是显示所有内容,让用户弄清楚应该填写什么,不应该填写什么。

Below is a good example.下面是一个很好的例子。 The [Record Matching Type] field has two options. [记录匹配类型] 字段有两个选项。 The first option (UniqueId) should only display [Unique ID Column Name].第一个选项 (UniqueId) 应该只显示 [Unique ID Column Name]。 The rest should be hidden.其余的应该隐藏。 How can this be done?如何才能做到这一点?

例子

This answer is very late;这个答案很晚; however, I use the Xceed property grid and have implemented what you have described.但是,我使用 Xceed 属性网格并实现了您所描述的内容。

First of all, if you do not want a property in your view model to ever display just add the [Browsable(false)] attribute above the property.首先,如果您不想在视图模型中显示某个属性,只需在该属性上方添加 [Browsable(false)] 属性即可。

Now, if you would like properties to be able to be shown/hidden based on the selection of another property you can do the following.现在,如果您希望能够根据对另一个属性的选择来显示/隐藏属性,您可以执行以下操作。

  1. Add this event to your view model.将此事件添加到您的视图模型。 public event Action<string, bool> ChangeVisible;

  2. Whenever you want a property to be shown or hidden you will raise this event.每当您想要显示或隐藏属性时,您都会引发此事件。 Ie You have an "Age" property and you want to hide the "Employer" property if the Age is less than 16. In the "Age" property setter you would check the value if (value) < 16 and then raise the event ChangeVisible?.Invoke("Employer", false);即您有一个“Age”属性,如果 Age 小于 16,您想隐藏“Employer”属性。在“Age”属性设置器中,您将检查值if (value) < 16 ,然后引发事件ChangeVisible?.Invoke("Employer", false); To show it instead of hiding you would pass 'true' instead of 'false'.要显示它而不是隐藏它,您将传递 'true' 而不是 'false'。

  3. For this to work you must also add a listener on the View that hosts the view model.为此,您还必须在承载视图模型的视图上添加一个侦听器。 In the constructor of the View (let's call it 'MyView.xaml.cs') you will register a listener on the DataContext (your view model).在视图的构造函数中(我们称之为“MyView.xaml.cs”),您将在 DataContext(您的视图模型)上注册一个侦听器。 MyViewModel.ChangeVisible += MyViewModel_ChangeVisible;

  4. In the listener you will directly call the 'ShowProperty' method of the PropertyGrid.在侦听器中,您将直接调用 PropertyGrid 的“ShowProperty”方法。 Let's say your PropertyGrid has a name of 'MyPropertyGrid'.假设您的 PropertyGrid 的名称为“MyPropertyGrid”。 Your listener would look like the following.您的听众将如下所示。

     /// <summary> /// Hide/show a preference /// </summary> /// <param name="property">name of the preference</param> /// <param name="show">make it visible or hide it?</param> private void MyViewModel_ChangeVisible(string property, bool show) { Dispatcher.Invoke(() => MyPropertyGrid.ShowProperty(property, show)); }
  5. Last, but not least, the PropertyGrid does not have a ShowProperty method.最后但同样重要的是,PropertyGrid 没有 ShowProperty 方法。 So you will create a static class, with a public static extension method, so you can make all this magic work.因此,您将创建一个带有公共静态扩展方法的静态类,这样您就可以使所有这些神奇的工作发挥作用。

     public static void ShowProperty(this PropertyGrid pg, string property, bool show) { for (int i = 0; i < pg.Properties.Count; ++i) { PropertyItem prop = pg.Properties[i] as PropertyItem; if (prop.PropertyName == property) { prop.Visibility = show ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed; break; } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM