我正在使用WPF扩展工具包来显示一个“团队”对象的属性。其中一个属性是一个名为“Persons”的集合。点击下拉框后,我可以看到每个人的名字和年龄。保留HTML标签。
更新
好的,@tencntraze的解决方案让我接近成功 - 谢谢。但是对于我这种情况下的对象集合,它不起作用。此外,可以简化它,使用CollectionControlDialog而不是下面实现的自定义ReadOnlyCollectionViewer。
XAML
现在的问题是,我实际上不想将我的 Collection 公开。但是,一旦我将其 setter 设为 private,该属性就会被禁用,防止用户查看 Person collection 和 person details:
当我的Person Collection的setter是私有的时,我应该如何显示它?我可以用XAML模板来实现吗?如果可以,怎么做?我正在使用MVVM,所以不想在代码后台中添加任何内容。更新
好的,@tencntraze的解决方案让我接近成功 - 谢谢。但是对于我这种情况下的对象集合,它不起作用。此外,可以简化它,使用CollectionControlDialog而不是下面实现的自定义ReadOnlyCollectionViewer。
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>
代码后台
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();
}
}