My application runs and renders correctly but I have noticed numerous errors in my debug output when the Window is loading. It's the same 3 errors for each item in an Itemscontrol which adds up to 100's.
The Itemscontrol DataTemplate has a Path object which has its DataContext bound to a property that is passed through an IValueConverter that returns an anonymous type which attributes on that Path Object bind to.
<Path Grid.Column="0" Grid.Row="0" Margin="5,1,2,1" StrokeThickness="2"
DataContext="{Binding Path=Value, Converter={StaticResource ShapeConverter},
IsAsync=True}" >
<Path.Data>
<Binding Path="Data" IsAsync="True" />
</Path.Data>
<Path.Fill>
<Binding Path="Fill" IsAsync="True" />
</Path.Fill>
<Path.Stroke>
<Binding Path="Stroke" IsAsync="True" />
</Path.Stroke>
</Path>
ShapeConverter is an IValueConverter that returns an anonymous type to bind to.
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int status = System.Convert.ToInt32(value);
var geometry = CreateShapeGeometry(status);
var strokecolor = GetStrokeColor(status);
var fillcolor = GetFillColor(status);
return new
{
Data = PathGeometry.CreateFromGeometry(geometry).ToString(),
Stroke = strokecolor,
Fill = fillcolor
};
}
I understand what the errors mean, as the properties it is looking for do not exist on Alarm, they exist on the Anonymous Type which it resolves and renders correctly.
What I don't understand is how to resolve BindingExpression path errors that aren't really errors. I am also wondering if this causing the screen to load somewhat slowly.
Here are the errors:
(Summary) BindingExpression path error: 'Fill', 'Stroke', 'Data' property not found on object Alarm.
(Actual) System.Windows.Data Error: 40 : BindingExpression path error: 'Fill' property not found on 'object' ''Alarm' (HashCode=37465686)'. BindingExpression:Path=Fill; DataItem='Alarm' (HashCode=37465686); target element is 'Path' (Name=''); target property is 'Fill' (type 'Brush')
System.Windows.Data Error: 40 : BindingExpression path error: 'Stroke' property not found on 'object' ''Alarm' (HashCode=37465686)'. BindingExpression:Path=Stroke; DataItem='Alarm' (HashCode=37465686); target element is 'Path' (Name=''); target property is 'Stroke' (type 'Brush')
System.Windows.Data Error: 40 : BindingExpression path error: 'Data' property not found on 'object' ''Alarm' (HashCode=37465686)'. BindingExpression:Path=Data; DataItem='Alarm' (HashCode=37465686); target element is 'Path' (Name=''); target property is 'Data' (type 'Geometry')
After some tinkering I'm not sure that this problem has anything to do with Anonymous types. I changed the convert method as follows but it didnt change my output.
public class Foo
{
public string Data { get; set; }
public SolidColorBrush Stroke { get; set; }
public SolidColorBrush Fill { get; set; }
}
Convert Method
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int status = System.Convert.ToInt32(value);
var geometry = CreateShapeGeometry(status);
var strokecolor = GetStrokeColor(status);
var fillcolor = GetFillColor(status);
Foo f = new Foo { Data = PathGeometry.CreateFromGeometry(geometry).ToString(), Stroke = strokecolor, Fill = fillcolor };
return f;
}
Alarm
object is theDataContext
at any point, if you can avoid that you will not get any errors. – H.B.DataType
only matters if you do not set thex:Key
(which then uses type based implicit application). By the way, you could use the attribute notation for those three property bindings (e.g.Data="{Binding Data}"
), also: UsingIsAsync
there is pointless as all three properties are instantly accessible as soon as the converter code returns. – H.B.