BindingExpression path error: property not found in 'object'

I searched for the clock of this error that appears in the output window. I'm new to WPF bindings, so I'm sure something is missing.

Full text of the error (there is one for each binding path, everything similar to this):

System.Windows.Data error: 39: BindingExpression path error: property 'TestItem' not found in 'object' '' String '(HashCode = -842352750)'. BindingExpression: Path = TestItem; DataItem = 'String' (HashCode = -842352750); target element is "TextBlock" (Name = ''); target is "Text" (type "String")

EDIT: Everything works as it should, but I get these errors in the output window.

XAML:

<UserControl> <UserControl.Resources> <c:MyData x:Key="myDataSource"/> <DataTemplate x:Key="image"> <Image x:Name="TheImage" /> <DataTemplate.Triggers> <DataTrigger Binding="{Binding Path=PassFail}" Value="PASS"> <Setter TargetName="TheImage" Property="Source" Value="Images/accept.png" /> </DataTrigger> <DataTrigger Binding="{Binding Path=PassFail}" Value="FAIL"> <Setter TargetName="TheImage" Property="Source" Value="Images/delete.png" /> </DataTrigger> <DataTrigger Binding="{Binding Path=PassFail}" Value="WARNING"> <Setter TargetName="TheImage" Property="Source" Value="Images/warning.png" /> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> <Storyboard x:Key="OnMouseLeftButtonDown1"/> </UserControl.Resources> <UserControl.DataContext> <Binding Source="{StaticResource myDataSource}"/> </UserControl.DataContext> <ListView Margin="0,94,-4,-7" x:Name="lsvwOutput" ItemsSource="{Binding Source={StaticResource myDataSource}}" MouseUp="lsvwOutput_MouseUp" FontFamily="Verdana"> <ListView.View> <GridView> <GridViewColumn Header="Test Item" Width="300" DisplayMemberBinding="{Binding Path=TestItem}" /> <GridViewColumn Header="Information" Width="0" DisplayMemberBinding="{Binding Path=Information}"/> <GridViewColumn Header="Result" Width="0" DisplayMemberBinding="{Binding Path=PassFail}"/> <GridViewColumn Header="Result" CellTemplate="{StaticResource image}" /> </GridView> </ListView.View> </ListView </UserControl> 

Code behind:

 public class MyData : INotifyPropertyChanged { private string _testitem = ""; private string _information = ""; private string _passfail = ""; public string TestItem { get { return _testitem; } set { _testitem = value; OnPropertyChanged("TestItem"); } } public string Information { get { return _information; } set { _information = value; OnPropertyChanged("Information"); } } public string PassFail { get { return _passfail; } set { _passfail = value; OnPropertyChanged("PassFail"); } } public string Text { get; set; } 
+8
c # wpf binding xaml
source share
2 answers

You do not want to set the DataContext in the UserControl. Instead, you want to set it in the UserControl area.

Usually you do this in the UserControl constructor. Usually I add a line like this:

 this.RootElement.DataContext = myData; 

Where RootElement is the first sub-element (Content) of your UserControl (usually a panel like Grid or StackPanel).

In your case, it will be:

this.lsvwOutput.DataContext = FindResource("myDataSource") ;

And make sure it is after calling InitializeComponent ().

This is just a review question. You set the datacontext on the root panel of the usercontrol. This is really an unobvious part of WPF.

UPDATE: As Marcus points out below, in the case of a list, you want to set an array of data, not just a data point. Keep this in mind when setting up a DataContext in your constructor.

+11
source share

it looks like you are binding your listviews itemssource to an object, not an array. Does everything work visually? or don't you see anything?

EDIT : what happens if you write instead:

 <c:MyData x:Key="myDataSource"/> 

 <x:Array x:Key="myDataSource" Type="{x:Type c:MyData}"> <c:MyData /> </x:Array> 

or any similar collection definition

+6
source share

All Articles