Home Forums WPF controls Other WPF controls PropertyGrid – How to set property values in code?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • aeastburg
    Participant
    Post count: 1
    #44255 |

    Given the following PropertyGrid what is the code to set a default Baudrate of 9600 in the PortPropertyGrid?
    More generally how does one set property values with code?

    
       <xctk:PropertyGrid x:Name="PortPropertyGrid" ></xctk:PropertyGrid>
    
       public partial class MainWindow : Window
       {
          private SerialPortSettings _serialPortSetting;
    
          public MainWindow()
          {
             InitializeComponent();
    
             _serialPortSetting = new SerialPortSettings();
    
             PortPropertyGrid.SelectedObject = _serialPortSetting;
    
            // Set a default Baudrate here.
          }
       }
    
       public class SerialPortSettings
       {
          [Description("The serial baud rate.")]
          [ItemsSource(typeof(BaudRateOptions))]
          public List<int> Baudrate { get; set; }
       }
    
       public class BaudRateOptions : IItemsSource
       {
          public ItemCollection GetValues()
          {
             var baudRateOptions = new ItemCollection
             {
                110, 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200
             };
    
             return baudRateOptions;
          }
       }
    
    Fawzi [Xceed]
    Member
    Post count: 722

    Hi,

    First, the property SerialSettings.Baudrate should not be of type “List<int>” since the possible values are individual integers. The property type should be of type “int”.
    Then, to set the default value of this property, you should set it before assigning the created object (_serialPortSetting) to the PropertyGrid.SelectedObject. Something like :
    InitializeComponent();

    _serialPortSetting = new SerialPortSettings();
    // Set a default Baudrate here.
    _serialPortSetting.Baudrate = 1200;
    PortPropertyGrid.SelectedObject = _serialPortSetting;

    If you want to set the default value of the property after the assignment to PropertyGrid.SelectedObject, you need to make sure your SerialPortSettings class is implementing the INotifyPropertyChanged interface. This way, any changes made in SerialPortSettings class will notify the PropertyGrid. Something like :

    public class SerialPortSettings : INotifyPropertyChanged
    {
    [Description( “The serial baud rate.” )]
    [ItemsSource( typeof( BaudRateOptions ) )]
    public int Baudrate
    {
    get
    {
    return _baudrate;
    }
    set
    {
    _baudrate = value;
    OnPropertyChanged( “Baudrate” );
    }
    }
    private int _baudrate;

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged( string propertyName )
    {
    if( PropertyChanged != null )
    {
    PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
    }
    }
    }

    Please note that starting at v3.2, you will be able to set the DefaultValue attribute to set your property’s default value. Something like :
    [Description( “The serial baud rate.” )]
    [ItemsSource( typeof( BaudRateOptions ) )]
    [DefaultValue(1200)]
    public int Baudrate { … }

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.