我将只读属性绑定到 PropertyGrid,我希望用户能够从单元格中选择和复制文本。我需要使用不同的属性吗?如果是这样,我应该使用哪个?或者也许有不同的解决方案?
谢谢,
我将只读属性绑定到 PropertyGrid,我希望用户能够从单元格中选择和复制文本。我需要使用不同的属性吗?如果是这样,我应该使用哪个?或者也许有不同的解决方案?
谢谢,
请不要在属性上使用 [ReadOnly(true)]。但是可以通过将其设置为 ReadOnly TextBox 来限制在 xceed 属性网格右侧编辑属性的值,如下所示:
.xaml:
<xctk:PropertyGrid x:Name="MyPrpGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
FontWeight="ExtraBold" ShowSearchBox="False" ShowSortOptions="False"
SelectedObject="{Binding BindedPropName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Background="#FF4A5D80" Foreground="White" >
<xctk:PropertyGrid.EditorDefinitions>
<!-- For Id, Text and Type fields at left-side in the xceed property grid, replace right-side value area with ReadOnly TextBox -->
<xctk:EditorDefinition>
<xctk:EditorDefinition.PropertiesDefinitions>
<xctk:PropertyDefinition Name="Id"/> <!-- BindedPropName.Id -->
<xctk:PropertyDefinition Name="Text" /> <!-- BindedPropName.Text -->
<xctk:PropertyDefinition Name="Type" /> <!-- BindedPropName.Type -->
</xctk:EditorDefinition.PropertiesDefinitions>
<xctk:EditorDefinition.EditorTemplate>
<DataTemplate>
<!-- Binded Value is a DependencyProperty which is defined in view model -->
<TextBox IsReadOnly="True" Text="{Binding Value}" BorderThickness="0"/>
</DataTemplate>
</xctk:EditorDefinition.EditorTemplate>
</xctk:EditorDefinition>
</xctk:PropertyGrid.EditorDefinitions>
</xctk:PropertyGrid>
MyViewModel.cs 应该从 DependencyObject 派生。
MyViewModel.cs:
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(MyViewModel),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string Value
{
get { return (string)GetValue(ValueProperty); } // GetValue() is from derived DependencyObject class
set { SetValue(ValueProperty, value); } // SetValue() is from derived DependencyObject class
}