在PropertyGrid控件中显示只读属性

问题描述 投票:7回答:2

我正在使用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,0,2,0" />
        </Button>
        <Label Name="CollectionLabel" Content="(Collection)" Padding="2,2,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();
    }
}
c# wpf xaml mvvm wpftoolkit
2个回答
10
投票

我认为你最好的选择是按照Xceed Documentation实现你自己的编辑器。然后,您可以向用户提供要显示的任何UI,而无需将值提交回基础对象。请注意,此方法适用于私有setter以及没有任何setter的属性。

ReadOnlyCollection编辑器

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, 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)
    {
        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,所以我只是展示了单击按钮的对话框。


1
投票
public Collection<Person> People
{
    get { return _people; }
    set { throw new NotSupportedException(); }
}

也许不是最好的解决方案,但它可以与PropertyGrid一起使用,同时阻止用户设置新的集合。

© www.soinside.com 2019 - 2024. All rights reserved.