Quantcast
Channel: wpftoolkit Discussions Rss Feed
Viewing all articles
Browse latest Browse all 2157

New Post: [Propertygrid] Show/Hide Properties depending on enum

$
0
0
Hi,
in v2.9, you can register to event "PropertyGrid.IsPropertyBrowsable". On the initialisation of each PropertyItem, this event will be called. Into it, you can tell if a PropertyItem is visible or not depending on your "SelectedBackupOption" property.
You then only need to add the attribute "RefreshProperties" on your SelectedBackupOption property to refresh the propertyGrid when this property is modified :
<xctk:PropertyGrid SelectedObject="{Binding}"
                         IsPropertyBrowsable="PropertyGrid_IsPropertyBrowsable"/>



 public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      this.DataContext = new DbConnection()
      {
        DailyBackupOptions = new DailyBackupOptions() { EveryXDays = 5 },
        WeeklyBackupOptions = new WeeklyBackupOptions() { EveryXWeeks = 4, DaysOfTheWeek = new List<DayOfWeek>() { DayOfWeek.Friday, DayOfWeek.Saturday } },
        SelectedBackupOption = BackupOption.Daily
      };
    }

    private void PropertyGrid_IsPropertyBrowsable( object sender, IsPropertyBrowsableArgs e )
    {
      if( e.PropertyDescriptor != null )
      {
        if( e.PropertyDescriptor.Name == "DailyBackupOptions" )
        {
          if( ((DbConnection)this.DataContext).SelectedBackupOption == BackupOption.Weekly )
          {
            e.IsBrowsable = false;
          }
        }
        else if( e.PropertyDescriptor.Name == "WeeklyBackupOptions" )
        {
          if( ((DbConnection)this.DataContext).SelectedBackupOption == BackupOption.Daily )
          {
            e.IsBrowsable = false;
          }
        }
      }
    }
  }

  public class DbConnection
  {
    [RefreshProperties( RefreshProperties.All )]
    public BackupOption SelectedBackupOption
    {
      get; set;
    }

    public DailyBackupOptions DailyBackupOptions
    {
      get; set;
    } = new DailyBackupOptions();

    public WeeklyBackupOptions WeeklyBackupOptions
    {
      get; set;
    } = new WeeklyBackupOptions();
  }

  public class DailyBackupOptions
  {
    public int EveryXDays
    {
      get; set;
    }
  }

  public class WeeklyBackupOptions
  {
    public List<DayOfWeek> DaysOfTheWeek
    {
      get; set;
    } = new List<DayOfWeek>();

    public int EveryXWeeks
    {
      get; set;
    }
  }

  public enum BackupOption
  {
    Daily,
    Weekly
  }

Viewing all articles
Browse latest Browse all 2157

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>