c# – 在PropertyGrid控件中显示只读属性

c# – 在PropertyGrid控件中显示只读属性,第1张

概述我正在使用 WPF扩展工具包来显示Team对象的属性.现在其中一个属性是人物集合.没问题,我得到一个很好的下拉,当我点击时,向我显示每个人的姓名和年龄. 现在的问题是我实际上并不希望将我的Collection公开为public.但是,只要我将其setter设为私有,就会禁用该属性,以防止用户看到Person集合和人员详细信息: 当它的setter是私有的时候我应该如何显示我的Person Coll 我正在使用 WPF扩展工具包来显示Team对象的属性.现在其中一个属性是人物集合.没问题,我得到一个很好的下拉,当我点击时,向我显示每个人的姓名和年龄.

现在的问题是我实际上并不希望将我的Collection公开为public.但是,只要我将其setter设为私有,就会禁用该属性,以防止用户看到Person集合和人员详细信息:

当它的setter是私有的时候我应该如何显示我的Person Collection?我可以使用XAML模板执行此 *** 作吗?如果是这样的话?我正在使用MVVM,所以我不想在代码中添加任何内容.

更新

好的,所以@tencntraze的解决方案让我大部分都在那里 – 谢谢.
但它对于对象的集合不起作用,这是我在我的情况下得到的.此外,它还可以简化为使用CollectionControlDialog而不是下面实现的自定义ReadonlyCollectionVIEwer.

XAML

<UserControl x:Class="DevExpresstreeList.ReadonlyCollectionEditor"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             x:name="MyUserControl"             >    <DockPanel>        <button Click="button_OnClick" DockPanel.Dock="Right">            <Label Content="˅" padding="2,2,0" />        </button>        <Label name="CollectionLabel" Content="(Collection)" padding="2,0" />    </DockPanel></UserControl>

代码隐藏

public partial class ReadonlyCollectionEditor : UserControl,ITypeEditor{    public ReadonlyCollectionEditor()    {        InitializeComponent();    }    // Use typeof(object) to allow for any Collection<T>    public static Readonly DependencyProperty ValueProperty = DependencyProperty.Register(        "Value",typeof(object),typeof(ReadonlyCollectionEditor),new PropertyMetadata(default(object)));    public object Value    {        // We are Now using object so no need to cast        get { return GetValue(ValueProperty); }        set { SetValue(ValueProperty,value); }    }    public FrameworkElement ResolveEditor(Xceed.Wpf.Toolkit.PropertyGrID.PropertyItem propertyItem)    {        var binding = new Binding("Value")        {            Source = propertyItem,Mode = propertyItem.IsReadonly ? BindingMode.OneWay : BindingMode.TwoWay        };        BindingOperations.SetBinding(this,ValueProperty,binding);        return this;    }    private voID button_OnClick(object sender,RoutedEventArgs e)    {        var collectionControlDialog = new CollectionControlDialog        {            ItemsSource = (IList)this.Value        };        collectionControlDialog.ShowDialog();    }}
解决方法 我认为你最好的选择是按照 Xceed Documentation实现自己的编辑器.然后你可以提供你想要显示给用户的任何UI,而不需要将值提交回底层对象.请注意,此方法适用于私有setter以及没有任何setter的属性.

ReadonlyCollectionEditor

XAML

<UserControl x:Class="WpfApplication2.ReadonlyCollectionEditor"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             x:name="uc">    <button Click="button_OnClick" Height="20" /></UserControl>

代码隐藏

public partial class ReadonlyCollectionEditor : UserControl,ITypeEditor{    public ReadonlyCollectionEditor()    {        InitializeComponent();    }    public static Readonly DependencyProperty ValueProperty = DependencyProperty.Register(        "Value",typeof (IList<string>),typeof (ReadonlyCollectionEditor),new PropertyMetadata(default(IList<string>)));    public IList<string> Value    {        get { return (IList<string>)GetValue(ValueProperty); }        set { SetValue(ValueProperty,RoutedEventArgs e)    {        ReadonlyCollectionVIEwer vIEwer = new ReadonlyCollectionVIEwer {DataContext = this};        vIEwer.ShowDialog();    }}

ReadonlyCollectionVIEwer

<Window x:Class="WpfApplication2.ReadonlyCollectionVIEwer"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        title="ReadonlyCollectionVIEwer" Height="300" WIDth="300">    <ListBox ItemsSource="{Binding Value}" /></Window>

示例属性类

public class MyDataObjects{    public MyDataObjects()    {        this.CollectionProperty = new Collection<string> {"Item 1","Item 2","Item 3"};                    this.StringProperty = "Hi!";    }    public string StringProperty { get; set; }    [Editor(typeof(ReadonlyCollectionEditor),typeof(ReadonlyCollectionEditor))]    public ICollection<string> CollectionProperty { get; private set; } }

分配给属性网格

this.propertyGrID.Selectedobject = new MyDataObjects();

结果

编辑

我意识到你想要使用MVVM,我在使用WPF时非常鼓励,但是为了这个示例的目的,我相信保持简单有助于说明这一点,否则它会带来其他问题,如showing a modal dialog from MVVM,所以我只是展示了单击按钮的对话框.

总结

以上是内存溢出为你收集整理的c# – 在PropertyGrid控件中显示只读属性全部内容,希望文章能够帮你解决c# – 在PropertyGrid控件中显示只读属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1247110.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存