Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding Design Time "Property Expected" error

I'm trying to bind an observable collection to a WPF DataGrid. One of my properties is a bool. I dislike DataGrid's CheckboxColumn, so I rolled my own with a TemplateColumn, which I bind to a public property on my DataContext. The project designs, compiles, and runs fine. However, in the designer, Visual Studio 2013 Professional underlines the binding path in red. When I hover over it, it says "Property Expected". Strangely enough, this does not show up in the Error List but the scrollbar DOES get the red "error marker" on it. Additionally, if I use a "standard" CheckboxColumn, VS doesn't show the underline.

Here's my DataContext's class:

sealed class Connection : IDisposable
{
    public bool Log { get; set; }

    public int HashCode { get; private set; }
}

And this is the DataGrid's XAML:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Connections}" SelectionMode="Single">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=HashCode, StringFormat={}{0:X}}" Header="ID" IsReadOnly="True" Width="50*"/>
        <DataGridTemplateColumn Header="Log">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding Path=Log, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Here's a screenshot of the error:

Error

Strangely enough, VS is fine with this XAML, which I don't want to use due to the behavior of the column:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Connections}" SelectionMode="Single">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=HashCode, StringFormat={}{0:X}}" Header="ID" IsReadOnly="True" Width="50*"/>
        <DataGridCheckBoxColumn Binding="{Binding Path=Log, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Log" />
    </DataGrid.Columns>
</DataGrid>

As I said, it compiles and runs fine. Is this a bug in Visual Studio?

like image 538
Xcelled Avatar asked Apr 10 '26 16:04

Xcelled


1 Answers

Try specifying your data type for the DataTemplate like this:

<DataTemplate DataType="wpfApplication1:Connection">
    <CheckBox ... />
</DataTemplate>
like image 142
Frank Avatar answered Apr 12 '26 06:04

Frank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!