Good day!

The second day I struggle in studying WPF PropertyGrid from xceed. There is little documentation on it, as well as my knowledge on WPF, but I would like to solve this problem.

Created an object, placed it in a PropertyGrid, implemented its own editor of property fields for an object. Records are displayed in the editor, I successfully select them, but they are not attached to the object property fields.

Test case below.

Property editor

<UserControl x:Class="TestExtendedWPFTools.DropDownControl" 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" d:DesignHeight="30" d:DesignWidth="300" x:Name="_uc"> <StackPanel> <ComboBox x:Name="cb" Text="{Binding Value, ElementName=_uc}"></ComboBox> </StackPanel> </UserControl> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Xceed.Wpf.Toolkit.PropertyGrid.Editors; using Xceed.Wpf.Toolkit.PropertyGrid; namespace TestExtendedWPFTools { /// <summary> /// Π›ΠΎΠ³ΠΈΠΊΠ° взаимодСйствия для DropDownControl.xaml /// </summary> public partial class DropDownControl : UserControl, ITypeEditor { public DropDownControl() { InitializeComponent(); for (var i = 0; i < 10; i++) { cb.Items.Add(new Field() { Key = i.ToString(), Value = string.Format("Record: {0}", i), }); } } public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(Field), typeof(DropDownControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); public Field Value { get { return (Field)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, DropDownControl.ValueProperty, binding); return this; } } } 

Editable object

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace TestExtendedWPFTools { public class DBInfo { [Editor(typeof(DropDownControl), typeof(DropDownControl))] public Field Access { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestExtendedWPFTools { public class Field { public string Key { get; set; } public string Value { get; set; } //public override string ToString() //{ // return Value; //} } } 

Just in case, the main window of the program

 <Window x:Class="TestExtendedWPFTools.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:xc="clr-namespace:Xceed.Wpf.Toolkit.PropertyGrid;assembly=Xceed.Wpf.Toolkit" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Button Width="200" Height="32" Click="Button_Click">ВСстовая ΠΊΠ½ΠΎΠΏΠΊΠ°</Button> <xc:PropertyGrid x:Name="grid" Grid.Row="1" AutoGenerateProperties="True" ShowSearchBox="False"> </xc:PropertyGrid> </Grid> </Window> /// Π›ΠΎΠ³ΠΈΠΊΠ° взаимодСйствия для MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); dbinfo = new DBInfo(); grid.SelectedObject = dbinfo; } public DBInfo dbinfo { get; set; } private void Button_Click(object sender, RoutedEventArgs e) { if (dbinfo != null) { } } } } 
  • What do you want to do? - Ev_Hyper
  • @Ev_Hyper, I want to give the Propertygrid object, change its fields, and then save the object. The WinForms Standard PropertyGrid figured out a few years ago, and now there is a need to switch to wpf, here I am rewriting the code on it. - pincher1519
  • wrote the answer ... check it should work ... but it would be better if you read the WPF literature and rewrite the code normally - Ev_Hyper
  • @Ev_Hyper, thanks. I am reading now in a hurry a book, I understand the general principle, but there is little experience in writing, therefore, in some places it may turn out to be a bydlokod. As I rewrite, I will pull up knowledge and come back to correct what I have already written. - pincher1519
  • When something is not clear - ask, the site is for this purpose intended :) - Ev_Hyper

1 answer 1

Instead of using a binding for the Text property, use the SelectedItem property.

Those. replace:

 <ComboBox x:Name="cb" Text="{Binding Value, ElementName=_uc}"></ComboBox> 

on:

 <ComboBox x:Name="cb" SelectedItem="{Binding Value}" /> 
  • Yes, thanks, it works. Terribly stupid using the textbox example forwarded to the combobox ... - pincher1519