Search code examples
wpfwpftoolkitpropertygrid

Copy values from PropertyGrid control to clipboard from Microsoft's Extended WPF Toolkit™ Community Edition


I'm using PropertyGrid from the toolkit in subj and MVVM. I would like to copy properties values to clipboard from the PropertyGrid. The best way is just to select the cell content with the value to copy and copy it. Or using right click menu. I have no idea how to do it. Could you please help?


Solution

  • Found the solution, it's pretty simple and described in the homepage..

    First, you should create a user control, which represents required editor for the required property. I created my own editor, which is read only, due to built-in editors allow edit text:

    XAML

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

    Be sure to implement the Xceed.Wpf.Toolkit.PropertyGrid.Editors.ITypeEditor interface:

    Code behind

    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;
        }
    }
    

    2nd, in the model, you should specify the editor you just created for the required property using an attribute:

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

    Done. Simple as a brick..