我是 WPF 的新手,这可能是个愚蠢的问题,但是......)一个由 xceed wpf propertygrid 可视化的自定义属性类
public class ShopProperties
{
private readonly ObservableCollection<string> _cars = new ObservableCollection<string>();
[Category(@"CarsShop")]
[DisplayName(@"CarsCollection")]
public ObservableCollection<string> CarsCollection { get {return _cars;}}
[Browsable(false)]
private string SelectedCar {get; set;}
}
我需要使用什么最简单最好的 PropertyGrid 编辑器(或自定义编辑器)来从 CarsCollection 分配 SelectedCar 元素?
最佳答案
经过一些搜索和阅读http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid&referringTitle=Documentation 我认为在我的情况下至少有两种方式。
1。自定义 xctk:CollectionControl
并编辑 XAML。
<xctk:PropertyGrid Name="_generalPropertyGrid" DockPanel.Dock="Top"
ShowSearchBox="False" ShowSortOptions="False" ShowTitle="False" NameColumnWidth="120" BorderThickness="0">
<xctk:PropertyGrid.EditorDefinitions>
<xctk:EditorTemplateDefinition TargetProperties="CarsCollection">
<xctk:EditorTemplateDefinition.EditingTemplate>
<DataTemplate>
<xctk:CollectionControl SelectedItem="{Binding Path=SelectedCar}"/>
</DataTemplate>
</xctk:EditorTemplateDefinition.EditingTemplate>
</xctk:EditorTemplateDefinition>
</xctk:PropertyGrid.EditorDefinitions>
</xctk:PropertyGrid>
2。创建自己的 UserControl
来实现 ITypeEditor
请参阅 http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid&referringTitle=Documentation 中的数据通过 ComboBox
选择。我选择这种方式。
<UserControl x:Class="proj_namespace.CarSelector"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
x:Name="CarSelector"
d:DesignHeight="25" d:DesignWidth="200">
<Grid>
<ComboBox ItemsSource="{Binding Value, ElementName=CarSelector}" SelectionChanged="Selector_OnSelectionChanged"/>
</Grid>
</UserControl>
后面还有类代码:
public partial class CarSelector : ITypeEditor
{
public CarSelector()
{
InitializeComponent();
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(ObservableCollection<string>), typeof(CarSelector),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public FrameworkElement ResolveEditor(PropertyItem propertyItem)
{
var binding = new Binding("Value");
binding.Source = propertyItem;
binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
BindingOperations.SetBinding(this, ValueProperty, binding);
return this;
}
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender != null)
MainWindow.Instance._properties.SelectedCar = (sender as ComboBox).SelectedItem as string;
}
}
最后在属性上方添加自定义编辑器行
[Editor(typeof(CarSelector), typeof(CarSelector))]
public ObservableCollection<string> CarsCollection { get { return _securities; } }
关于c# - xceed wpf 属性网格集合选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35225505/