Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

Commented Unassigned: PropertyGrid: Collections of primitive types bug [21463]

$
0
0
Using the PropertyGrid control, if one of the properties is of type List<T> ou T[], where T is a primitve type, the user can enter the values he wants, but they get lost. I found a fix, at least for collections of type List<T>:

Change PrimitiveTypeCollectionEditor.cs, ResolveValueBinding() to:
{
var type = propertyItem.PropertyType;
Editor.ItemsSourceType = type;

if (type.BaseType == typeof(System.Array))
{
Editor.ItemType = type.GetElementType();
}
else // if (type.ContainsGenericParameters)
{
Editor.ItemType = type.GetGenericArguments()[0];
}

base.ResolveValueBinding( propertyItem );
}
}

Change PrimitiveTypeCollectionControl.cs, ComputeItemsSource() to:
{
if (ItemsSource == null)
{
string tt = Text;
ItemsSource = CreateItemsSource();
Text = tt;
}

return ItemsSource;
}

NB: This last change is necessary not to loose the value when the collection object needs to be created. Otherwise, the OnTextChanged event is called when the collection object gets created, and the desired content gets lost.
Comments: ** Comment from web user: Steph555 **

Hi BoucherS,

I did an exemple with a test class and a window. You will see that it works well for the integer, but not for the array of integers. However, it works with the fix I propose.

To use my exemple, enter some values in the property grid fields and click the button to show the content. What you will have entered for the array will not be shown, because it gets lost. There are more than one bug with these array fields, but my fix seems to fix most of them.

THE CLASS:
public class TestClass
{
public int theInt { get; set; }
public List<int> theIntArray { get; set; }
}

XAML:
<Grid>
<StackPanel Orientation="Vertical">
<xctk:PropertyGrid Name="theGrid"/>
<Button Content="Show the values:" Width="150" Margin="5" Click="Button_Click_1"></Button>
<Label Name="valueLabelInt" Width="100" Margin="5" BorderThickness="1" BorderBrush="Black"/>
<Label Name="valueLabelIntArray" Width="100" Margin="5" BorderThickness="1" BorderBrush="Black"/>
</StackPanel>
</Grid>

THE WINDOW CODE:
public partial class MainWindow : Window
{
public TestClass theTestClass { get; set; }
public MainWindow()
{
InitializeComponent();
theTestClass = new TestClass();
theGrid.SelectedObject = theTestClass;
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.valueLabelInt.Content = theTestClass.theInt;
if (theTestClass.theIntArray != null)
{
string s = "";
for (int i = 0; i < theTestClass.theIntArray.Count; i++)
s += theTestClass.theIntArray[i] + ", ";
this.valueLabelIntArray.Content = s;
}
}
}


Viewing all articles
Browse latest Browse all 4964

Latest Images

Trending Articles



Latest Images

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