PropertyGrid DataObject binding not working on view model load

Grids for WPF Forum

Posted 1 month ago by Mooreland
Version: 24.1.2
Platform: .NET 6.0
Environment: Windows 11 (64-bit)
Avatar

I have a property grid on my view model (XAML below.)  I have the DataObject property bound to a an object on my view (code below.)  When my viewmodel is loaded, the DataObject is not shown in the property grid.  If I live edit the XAML to delete and then add the binding back, it works fine.  It continues to work and update when the DataObject is changed from code behind.  It is only during the first load of the view model when it does not work. 

<grids:PropertyGrid
x:Name="ctlPropertyEditor"
Margin="0,10,0,0"
DataObject="{Binding Message}"
IsSummaryVisible="False"
IsCategorized="False"
CanClearDataObjectOnUnload="True"
AreReadOnlyPropertiesBrowsable="False"
gridseditors:BuiltinPropertyEditors.IsEnabled="True">
<grids:PropertyGrid.PropertyEditors>
<grids:PropertyEditor PropertyType="numerics:Time">
<grids:PropertyEditor.ValueTemplate>
<DataTemplate>
<editors:TimeSpanEditBox Value="{Binding Value, Converter={StaticResource TimeToTimeSpanConverter}}"/>
</DataTemplate>
</grids:PropertyEditor.ValueTemplate>
</grids:PropertyEditor>
</grids:PropertyGrid.PropertyEditors>
</grids:PropertyGrid>

public class MscCommandBuilderViewModel : NotifyingObject
{

public IMscMessage Message
{
get => _message;
set
{
if (value == _message) { return; }
_message = value;
NotifyOfPropertyChange();
}
}

public MscCommandBuilderViewModel(byte[] data)
{
Message = CreateMessage(data);
}

}

Comments (6)

Posted 1 month ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

I'm sorry you're having trouble.  The PropertyGrid.DataObject property is watched for updates and should work fine on load if your binding is functioning correctly and is returning the value you expect.  Perhaps bind your Message value to another control at the same UI hiearchy level to ensure it's passing through what you think it is on load.

Beyond that, you could make a custom IDataFactory that inherits the TypeDescriptorFactory class and assign it to the PropertyGrid.DataFactory property.  Then see what values are being passed in the IDataFactoryRequest sent to the IDataFactory.GetDataModels method at load.  That may help you determine where things are going wrong.


Actipro Software Support

Posted 1 month ago by Mooreland
Avatar

With a DoubleEditBox and a PropertyGrid in the same stack panel, the DoubleEditBox value property will bind correctly and the DataObject property of the PropertyGrid will not.  The IDataFactory method does not get called.  Changing the object that is the target of the binding and raising the property changed event also does not cause the IDataFactory method to be called.  So far, the only thing that works is deleting the binding "live" in XAML and renetering the exact same binding.  After that it works fine.  

I'm using this with an MVVM framework.  I am trying to replicate it in a small test app without the framework.  

  

Posted 1 month ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

That's very odd.  Yes, please try to reproduce it in a simple sample project, thanks.


Actipro Software Support

Posted 1 month ago by Mooreland
Avatar

I have not been able to reproduce the issue in a simple project yet, but I have confirmed that the binding is happening in the Presentation Framework.  The problem seems to be in the control.  At this time, I am assuming it is a layout or intialization issue and not specifically a binding problem.  

Posted 1 month ago by Mooreland
Avatar

The issue comes from "CanClearDataObjectOnUnload".  Setting this to "True" causes the binding issue when my FrameworkElement is created.  It appears that the "unload" event is being called on my FrameworkElement after it has been loaded prior to being reloaded.  I'm not entirely sure what it is being "unloaded" from prior to being "loaded" in the correct visual tree, but I assume that is what is permanently breaking the dataobject binding when "CanClearDataObjectOnUnload" is set to TRUE.  

[Modified 1 month ago]

The latest build of this product (v24.1.3) was released 1 month ago, which was before the next post in this thread.
Posted 1 month ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Without a sample to debug, it's tough for us to say what's going on in terms of what in your app tells PropertyGrid it's being unloaded.

Here is the information in the doccomments remarks of that CanClearDataObjectOnUnload property:

The CanClearDataObjectOnUnload property should not be set to true if a property grid or its ancestor hierarchy can get temporarily unloaded.  For instance, when a property grid is in a tab control, the property grid is unloaded when the parent tab is deselected.  Likewise, when a property grid is a docking window, the property grid is temporarily unloaded during dock operations as layout changes occur.  In these sorts of scenarios, it is better to manually clear the DataObject property when the primary UI (such as a main window) is closed.

Perhaps in your scenario, it's better to not use that property and simply set the PropertyGrid.DataObject to null when you know your containing window is closing.


Actipro Software Support

Add Comment

Please log in to a validated account to post comments.