risty
risty

Reputation: 43

xceed wpf propertygrid collection select

i'm new in WPF and may be it's stupid question, but...) a have custom properties class visualased by 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;}
}

What simplest and finest PropertyGrid editor(or custom editor) i need to use to assign SelectedCar element from CarsCollection?

Upvotes: 2

Views: 2986

Answers (2)

risty
risty

Reputation: 43

after some search and reading http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid&referringTitle=Documentation i think its minimum two ways in my case.

1. Customise xctk:CollectionControl and edit 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. Create own UserControl that implements the ITypeEditor

see datails in http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid&referringTitle=Documentation Select by ComboBox. i choose this way.

<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>

And class code behind:

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;
    }
}

and finally add custom editor line above property

[Editor(typeof(CarSelector), typeof(CarSelector))]
public ObservableCollection<string> CarsCollection { get { return _securities; } }

Upvotes: 2

KinSlayerUY
KinSlayerUY

Reputation: 1940

I don't know why you are using propertygrid instead of a datagrid, property grid is used to see and change the properties of an object in memory (like one of the elements of the WPF window), it's like the Property window in visual studio that shows all the properties of the selected object.

if you want to show a collection, even if you want to edit it afterwards, you should try xceed data grid, it will be more up your ally i think.

But anyways, you can bind the selectedProperty property of the propertygrid to the selected car, or you can use the selectedProperty property of propertygrid directly and check if it's a car.

Upvotes: -1

Related Questions