1
votes

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;
    }
2
the slowdown will only happen in DEBUG mode, in release mode there will be virtually no performance impact due to the errorsthumbmunkeys
@thumbmunkeys Are they actual errors I need to be concerned about at all? Is there a surpress tag for them at all? I'm just preparing for the possiblity of having to explain them to a build manager.jrandomuser
The question is why an Alarm object is the DataContext at any point, if you can avoid that you will not get any errors.H.B.
@H.B. I thought perhaps it was getting it from the DataTemplate declaration. <DataTemplate x:Key="PanelItemTemplate" DataType="{x:Type m:Alarm}" > but even after I took out the DataType attribute the message is the same.jrandomuser
@jrandomuser: DataType only matters if you do not set the x: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: Using IsAsync there is pointless as all three properties are instantly accessible as soon as the converter code returns.H.B.

2 Answers

0
votes

There are plenty of resource out there that talk about binding errors can reduce app performance.

See number 7 - http://blogs.msdn.com/b/visualstudio/archive/2010/03/02/wpf-in-visual-studio-2010-part-2-performance-tuning.aspx

0
votes

In the end I found that while you can create and bind to properties on an anonymous type or even an instance of a defined class that are returned from a Value Converter, as it goes to do the binding it will encounter but overcome debug errors.

Another approach in this case was to create a set of styles instead as static resources and then bind the Style Attribute of the Path object. See Binding for WPF Styles