ComboBox binding using DoubleCollection throwing ArgumentException
I am developing using Silverlight 3 and I have the following xaml:
<Application.Resources>
<DataTemplate x:Key="LineCombo">
<StackPanel Orientation="Horizontal" Margin="2">
<Line X1="0" Y1="0" X2="50" Y2="0" VerticalAlignment="Center" Stroke="Blue" StrokeThickness="1" StrokeDashArray="{Binding}" />
</StackPanel>
</DataTemplate>
</Application.Resources>
<StackPanel>开发者_运维问答;
<ComboBox x:Name="ComboBoxTest1" ItemTemplate="{StaticResource LineCombo}" Width="200" Height="30">
</ComboBox>
<ComboBox x:Name="ComboBoxTest2" ItemTemplate="{StaticResource LineCombo}" Width="200" Height="30">
</ComboBox>
</StackPanel>
And code behind:
public ObservableCollection<DoubleCollection> strokeDashArrays1 = new ObservableCollection<DoubleCollection>();
public ObservableCollection<Double[]> strokeDashArrays2 = new ObservableCollection<Double[]>();
public MainPage()
{
InitializeComponent();
strokeDashArrays1.Add(new DoubleCollection { 2, 4 });
strokeDashArrays1.Add(new DoubleCollection { 3, 6 });
strokeDashArrays1.Add(new DoubleCollection { 4, 8 });
strokeDashArrays2.Add(new double[] { 2, 4 });
strokeDashArrays2.Add(new double[] { 3, 6 });
strokeDashArrays2.Add(new double[] { 4, 8 });
ComboBoxTest1.ItemsSource = strokeDashArrays1;
ComboBoxTest2.ItemsSource = strokeDashArrays2;
}
Selecting an item from ComboBoxTest1 throws an ArgumentException:
"Value does not fall within the expected range".
Both display OK and ComboBoxTest2 works OK when selecting an item.
What is causing this behaviour?
I have only been working with Silverlight for a couple of weeks and am trying to display a ComboBox of StrokeDashArrays to allow line customisation.
Binding to the StrokeDashArray property works when using a string of values so I worked around this problem by implementing the following IValueConverter:
public class DoubleCollectionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DoubleCollection doubleCollection = (DoubleCollection)value;
return string.Join(",", doubleCollection.Select(x => x.ToString()).ToArray());
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Used like this:
<src:DoubleCollectionConverter x:Key="doubleCollectionConverter"/>
<DataTemplate x:Key="LineCombo">
<StackPanel Orientation="Horizontal" Margin="2">
<Line X1="0" Y1="0" X2="50" Y2="0" VerticalAlignment="Center" Stroke="Blue" StrokeThickness="1" StrokeDashArray="{Binding Converter={StaticResource doubleCollectionConverter}}" />
</StackPanel>
</DataTemplate>
Never figured out why the exception was being thrown though...
I have run into a similar problem using DoubleCollections. Typically, this "Value does not fall within the expected range" error indicates that "two objects having the same name in the same parent."
When using a DoubleCollection, I found that if I was certain I didn't use a common instance, I didn't get this error. Silverlight didn't like me trying to use the same instance of a DoubleCollection for between the different children.
Give this a try!
精彩评论