xceedsoftware / wpftoolkit

All the controls missing in WPF. Over 1 million downloads.
Other
3.9k stars 878 forks source link

Property Readonly Binding #1652

Open soyeon131013 opened 3 years ago

soyeon131013 commented 3 years ago

Hi, I am Joy.

I wonder one thing. Can I binding dynamically Property Grid's 'IsReadOnly' property? I couldn't find a way to dynamically bind to the 'IsReadOnly' property.

I will wait for your reply. Thank you.

XceedBoucherS commented 3 years ago

Hi, Yes, you should be able to bind the PropertyGrid.IsReadOnly property. Here's an example: ` <Button Content="TEST" Click="Button_Click" />

<xctk:PropertyGrid SelectedObject="{Binding MyPlayer}"
                   IsReadOnly="{Binding ReadOnly}"/>

`

` public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent();

  this.DataContext = this;

  this.MyPlayer = new Player()
  {
    FirtsName = "Tom", LastName = "Jones", Number = 25
  };
  this.ReadOnly = false;
}

public Player MyPlayer
{
  get;
  set;
}

public bool ReadOnly
{
  get
  {
    return _readOnly;
  }
  set
  {
    _readOnly = value;
    this.OnPropertyChanged( "ReadOnly" );
  }
}
private bool _readOnly;

private void Button_Click( object sender, RoutedEventArgs e )
{
  this.ReadOnly = !this.ReadOnly;
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged( string propertyName )
{
  var handler = this.PropertyChanged;
  if( handler == null )
    return;

  handler.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
}

}

public class Player { public string FirtsName { get; set; }

public string LastName
{
  get;
  set;
}

public int Number
{
  get;
  set;
}

}`

soyeon131013 commented 3 years ago

Hmm.. Your sample is different of my case. Let me show you the sample code.

<xctk:PropertyGrid SelectedObject="{Binding }" />

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

this.DataContext = ViewModel; }`

`public class ViewModel {

[Category("Player Info")] [DisplayName("First Name")] [ReadOnly(?)] // This is the point I asked. public string FirtsName { get; set; }

[Category("Player Info")] [DisplayName("Last Name")] public string LastName { get; set; }

[Category("Player Info")] [DisplayName("Number")] public int Number { get; set; } }`

My code is a little different from yours. What I'm curious about is whether the annotated part can binding dynamically. Or I wonder what other ways I can adjust the 'ReadOnly' of the contents of the property grid in the behind-the-scenes code.

Thank you.

XceedBoucherS commented 3 years ago

Hi, I don't see how a Property attribute can be bounded too. You can always register to the PropertyGrid.PreparePropertyItem and in the callback do whatever you want. This event will be raised while preparing the PropertyItem to display in the PropertyGrid. ` private void PropertyGrid_PreparePropertyItem( object sender, Xceed.Wpf.Toolkit.PropertyGrid.PropertyItemEventArgs e ) { var propertyItem = e.Item as PropertyItem; if( propertyItem != null ) { System.Diagnostics.Debug.WriteLine("property " + propertyItem.DisplayName + " has readOnly set to " + propertyItem.IsReadOnly );

    // Making sure all properties are ReadOnly.
    propertyItem.IsReadOnly = true;
  }
}`