wpftoolkit icon indicating copy to clipboard operation
wpftoolkit copied to clipboard

Property Readonly Binding

Open soyeon131013 opened this issue 4 years ago • 3 comments

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.

soyeon131013 avatar Mar 04 '21 06:03 soyeon131013

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

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

</StackPanel>`

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

}`

XceedBoucherS avatar Mar 04 '21 16:03 XceedBoucherS

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.

soyeon131013 avatar Mar 05 '21 01:03 soyeon131013

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

XceedBoucherS avatar Mar 08 '21 17:03 XceedBoucherS