I am using the WPF Extended Toolkit to display the properties of a Team object. Now one of these properties is a collection Persons. No problem I get a nice drop down, which when I click on shows me the names and ages of each of these people.
Now the problem is that I don't actually want to expose my Collection as public. However as soon as I make its setter private the property is disabled preventing the user from seeing the Person collection and the person details:
How should I display my Person Collection when its setter is private? Can I do this with a XAML template? If so how? I'm using MVVM so I don't want to put anything in the code behind.
Update
OK so the solution by @tencntraze got me most of the way there - thanks. However it doesn't work for Collections of objects which is what I've got in my case. In addition it can also be simplified to use the CollectionControlDialog instead of the custom ReadOnlyCollectionViewer that's been implemented below.
XAML
<UserControl x:Class="DevExpressTreeList.ReadOnlyCollectionEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="MyUserControl"
>
<DockPanel>
<Button Click="Button_OnClick" DockPanel.Dock="Right">
<Label Content="˅" Padding="2,0,2,0" />
</Button>
<Label Name="CollectionLabel" Content="(Collection)" Padding="2,2,2,0" />
</DockPanel>
</UserControl>
Code-Behind
public partial class ReadOnlyCollectionEditor : UserControl, ITypeEditor
{
public ReadOnlyCollectionEditor()
{
InitializeComponent();
}
// Use typeof(object) to allow for any Collection<T>
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(object), typeof(ReadOnlyCollectionEditor), new PropertyMetadata(default(object)));
public object Value
{
// We are now using object so no need to cast
get { return 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)
{
var collectionControlDialog = new CollectionControlDialog
{
ItemsSource = (IList)this.Value
};
collectionControlDialog.ShowDialog();
}
}