将值从PropertyGrid控件复制到Microsoft的Extended WPF Toolkit™Community Edition中的剪贴板

时间:2012-12-25 08:28:32

标签: wpf wpftoolkit propertygrid

我正在使用subj和MVVM中的工具包中的PropertyGrid。我想从PropertyGrid将属性值复制到剪贴板。最好的方法是选择具有复制和复制值的单元格内容。或使用右键菜单。我不知道该怎么做。你能帮忙吗?

2 个答案:

答案 0 :(得分:3)

找到解决方案,它非常简单,并在主页中进行了描述..

首先,您应该创建一个用户控件,该控件表示所需属性的必需编辑器。我创建了自己的编辑器,它是只读的,因为内置编辑器允许编辑文本:

<强> XAML

    <UserControl x:Class="WhiteRepositoryExplorer.Views.ReadOnlyTextEditor"
            ...
    x:Name="_uc">
        <xctk:AutoSelectTextBox IsReadOnly="True" Text="{Binding Value, ElementName=_uc}" AutoSelectBehavior="OnFocus" BorderThickness="0" />
    </UserControl>

请务必实施Xceed.Wpf.Toolkit.PropertyGrid.Editors.ITypeEditor界面:

代码

public class ReadOnlyTextEditor : UserControl, ITypeEditor 
{
    public ReadOnlyTextEditor()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string),
        typeof(ReadOnlyTextEditor), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public string Value
    {
        get { return (string)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    public FrameworkElement ResolveEditor(PropertyItem propertyItem)
    {
        Binding binding = new Binding("Value");
        binding.Source = propertyItem;
        binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
        BindingOperations.SetBinding(this, ValueProperty, binding);
        return this;
    }
}

第二,在模型中,您应该使用属性指定刚为所需属性创建的编辑器:

[Editor(typeof(ReadOnlyTextEditor), typeof(ReadOnlyTextEditor))]
[Description("Unique (int the XML scope) control attribute of string format")]
public string Id
{
    get { return _id; }
}

完成。简单如砖......

答案 1 :(得分:0)

这项工作不适用于只读属性...... 只有已知的解决方法是空的Setter。

[Editor(typeof(ReadOnlyTextEditor), typeof(ReadOnlyTextEditor))]
public string FullName
{
    get
    {
        if (FunctionGroupParent != null)
        {
            return FunctionGroupParent.FullName + "." + Name;
        }
        else
        {
            return Masterdata.Name + "." + Name;
        }
    }
    set
    {

    }
}

UC应该具有Padding(2px left)和Foreground(Gray)的属性以匹配属性网格的样式。

<xctk:AutoSelectTextBox Text="{Binding Value, ElementName=_uc}"
                        IsReadOnly="True" 
                        Padding="2,0,0,0"
                        Foreground="Gray"
                        AutoSelectBehavior="OnFocus"
                        BorderThickness="0" />