Displaying read only properties in PropertyGrid control
I think that your best bet here is to implement your own editor, as per the Xceed Documentation. You are then able to provide whatever UI you would like to display to the user without needing to commit the values back to the underlying object. Note that this approach works for both private setters as well as properties without any setter.
ReadOnlyCollectionEditor
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>
Code-Behind
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>
Sample Properties Class
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; }
}
Assigning to the property grid
this.propertyGrid.SelectedObject = new MyDataObjects();
Results
I realize that you want to use MVVM, which I strongly encourage when using WPF, but for purposes of this sample I believe that keeping it simple helps illustrate the point, otherwise it brings up other questions like showing a modal dialog from MVVM, so I'm just showing the dialog with a button click.