I have faced strange behavior in wpf. When ever I try to pass x:Array
as converter parameter to my converter I get this error. and designer view shows error instead of my controls.
this error only happens when I do this in Style
:
property 'System.Windows.Data.Binding'.'ConverterParameter' is null.
While my program does compile and run successfully without any problems, but its really annoying in design view because I cant see my controls, I hope you can find the reason of this problem and give solution.
I have simplified what I had and following is the way to reproduce this problem (so this is just example, not exactly what I have).
You can reproduce this problem simply by doing these steps:
Step 1 : make a user control with this content. (ContentControl is just example. this happens to any control that has template property.)
<UserControl.Resources> <!-- will tell you in next step --> </UserControl.Resources>
<Grid>
<ContentControl Style="{StaticResource MyStyle}"></ContentControl>
</Grid>
Step2: Now you have to create MyStyle
in your UserControl resource (or any global resource you like).
<UserControl.Resources>
<Style x:Key="MyStyle" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Border Background="Black" Width="16" Height="16">
<!-- if I remove these error will be gone -->
<Border.Resources>
<converters:ThicknessConverter x:Key="ThicknessConverter"/>
<x:Array x:Key="ThicknessModifier" Type="Thickness">
<!--Thickness Coefficient-->
<Thickness>-0.5</Thickness>
<!--Thickness Offset-->
<Thickness>0,2</Thickness>
</x:Array>
</Border.Resources>
<Border.Margin>
<Binding Path="Width"
RelativeSource="{RelativeSource Self}"
Converter="{StaticResource ThicknessConverter}"
ConverterParameter="{StaticResource ThicknessModifier}"/>
</Border.Margin>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
Step3: Create the converter, I haven't tested but I think this will happen to any converter. because my converter doesn't throw exception.
/// <summary>
/// returns thickness and sets given value to uniform length,
/// array of thickness with length of two can be passed as parameter,
/// first element of parameter is used for thickness multiplication and second element is used for addition.
/// note that multiplication has higher priority.
/// </summary>
[ValueConversion(typeof(double), typeof(Thickness), ParameterType = typeof(Thickness[]))]
public sealed class ThicknessConverter : IValueConverter
{
[NotNull]
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double)
{
var val = (double)value;
var param = parameter as Thickness[];
if (param?.Length >= 1)
{
if (param.Length >= 2)
{
return new Thickness(
val*param[0].Left + param[1].Left,
val*param[0].Top + param[1].Top,
val*param[0].Right + param[1].Right,
val*param[0].Bottom + param[1].Bottom);
}
else
{
return new Thickness(
val*param[0].Left,
val*param[0].Top,
val*param[0].Right,
val*param[0].Bottom);
}
}
else
{
if (parameter is Thickness)
{
var mul = (Thickness)parameter;
return new Thickness(
val*mul.Left,
val*mul.Top,
val*mul.Right,
val*mul.Bottom);
}
return new Thickness(val);
}
}
else return default(Thickness);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Step4: Put your user control in main window. and run the program. and I hope you see the error. I did reproduce this in my computer so I think you should get that too. How can I fix this problem?