xceedsoftware / wpftoolkit

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

PropertyGrid in ItemsControl ItemTemplate is always empty #1756

Open LJN-hzleaper opened 1 year ago

LJN-hzleaper commented 1 year ago

I bind PropertyGrid(in ListBox ItemTemplate) PropertyDefinitions to a PropertyDefinitionCollection in my viewmodel, but I find the binding does not work, the PropertyDefinitionCollection of all PropertyGrid is empty, and all PropertyGrid are empty. Here is my code:

<ListBox ItemsSource="{Binding  ManufacturerList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <xctk:PropertyGrid
                SelectedObject="{Binding }"
                AutoGenerateProperties="False"
                PropertyDefinitions="{Binding DataContext.PropertyDefinitionCollection, ElementName=window}">
            </xctk:PropertyGrid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
public class Company
{
    public string Task { get; set; }
    public string HeadNote { get; set; }
}
public class ViewModel
{
    public PropertyDefinitionCollection PropertyDefinitionCollection { get; set; } =
        new PropertyDefinitionCollection()
        {
            new PropertyDefinition()
            {
                TargetProperties = new[] { "Task" }
            },
            new PropertyDefinition()
            {
                TargetProperties = new[] { "HeadNote" }
            }
        };

    public List<Company> ManufacturerList { get; set; }

    public ViewModel()
    {
        List<Company> list = new List<Company>();

        list.Add(new Company()
        {
            Task = "Coding",
            HeadNote = "Coding Task",

        });

        list.Add(new Company()
        {
            Task = "Communicate",
            HeadNote = "Communicate Task",
        });

        ManufacturerList = list;
    }
}
XceedBoucherS commented 10 months ago

Hi,

This issue has been fixed. The fix will be part of v4.7.

In the meantime, you can go in file Xceed.Wpf.Toolkit/PropertyGrid/Implementation/PropertyGrid.cs, in the constructor, and replace: PropertyDefinitions = new PropertyDefinitionCollection(); with: this.SetCurrentValue( PropertyGrid.PropertyDefinitionsProperty, new PropertyDefinitionCollection() );

Thank you