2

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

4

3 回答 3

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

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

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

完毕。像砖一样简单。。

于 2013-01-02T09:42:57.837 回答
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" />
于 2015-05-21T07:53:02.507 回答
0

这可能只是多年来图书馆的变化,但作者的回答只是为我显示了一个空白条目,至少对于只读属性。这个更简单的版本通过使用作者提到PropertyGrid的相同属性来定位特定属性,对我有用:AutoSelectTextBox

<xctk:PropertyGrid.EditorDefinitions>
    <xctk:EditorTemplateDefinition TargetProperties="Altitude">
        <xctk:EditorTemplateDefinition.EditingTemplate>
            <DataTemplate>
                <xctk:AutoSelectTextBox IsReadOnly="True" Text="{Binding Value}" AutoSelectBehavior="OnFocus" BorderThickness="0" />
            </DataTemplate>
        </xctk:EditorTemplateDefinition.EditingTemplate>
    </xctk:EditorTemplateDefinition>
</xctk:PropertyGrid.EditorDefinitions>

于 2021-10-02T12:43:11.943 回答