【问题标题】:xceed wpf propertygrid collection selectxceed wpf propertygrid 集合选择
【发布时间】:2016-02-05 13:32:19
【问题描述】:

我是 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 元素?

【问题讨论】:

    标签: c# wpf collections propertygrid xceed


    【解决方案1】:

    经过一番搜索和阅读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; } }
    

    【讨论】:

      【解决方案2】:

      我不知道你为什么用propertygrid而不是datagrid,property grid是用来查看和更改内存中对象的属性(就像WPF窗口的元素之一),它就像Property window在 Visual Studio 中显示所选对象的所有属性。

      如果你想展示一个集合,即使你想在之后编辑它,你应该尝试 xceed 数据网格,我认为它会更适合你。

      不过不管怎样,你可以将propertygrid的selectedProperty属性绑定到选中的车上,也可以直接使用propertygrid的selectedProperty属性,判断是不是车。

      【讨论】:

      • 历史上选择使用propertygrid而不是datagrid来可视化属性=(我同意你的观点,在这种情况下数据网格会很友好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 2019-12-24
      相关资源
      最近更新 更多