4
votes

I've WPF applcation in which I usedDataBinding for a comboBox. ProjectName from projectList should add inside my comboBox, but when I run the app, every time I get these errors;

System.Windows.Data Error: 40 : BindingExpression path error: 'projectList' property not found on 'object' ''DayView' (Name='MainWin')'. BindingExpression:Path=projectList; DataItem='DayView' (Name='MainWin'); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') System.Windows.Data Error: 40 : BindingExpression path error: 'selectedProjectid' property not found on 'object' ''ComboBox' (Name='')'. BindingExpression:Path=selectedProjectid; DataItem='ComboBox' (Name=''); target element is 'ComboBox' (Name=''); target property is 'SelectedValue' (type 'Object')

My xaml code where I use Data Binding is:

<DataTemplate x:Key="EditableDataTemplate">
            <StackPanel Orientation="Horizontal" Width="596">
                <TextBox Text="{Binding ClientNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
                <TextBox Text="{Binding ApplicationNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
                <TextBox Text="{Binding StartTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
                <TextBox Text="{Binding StopTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
                <TextBox Text="{Binding TaskNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
                <ComboBox x:Name="ComboBox2" ItemsSource="{Binding Path=projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedValue="{Binding Path=selectedProjectid}" Width="71" Background="Yellow" BorderThickness="0" DataContext="{Binding RelativeSource={RelativeSource Self}}"/>
            </StackPanel>
        </DataTemplate>

Code behind is:

public partial class DayView : MetroWindow
{
    private DateTime currentDateForWindow;

    public List<Harvest_Project> projectList;

    public int selectedProjectid{get;set;}

    public DayView(DateTime s)
    {
            InitializeComponent();
             this.DataContext = projectList;
            //this.RootElement.DataContext = myData;
            Globals._globalController.setDayViewWindow(this);

            currentDateForWindow = s;

            dayDateLabel.Content = s.DayOfWeek + ", " + s.Day;
            monthLabel.Content = s.ToString("MMMM");

            listBox1.Items.Clear();

            //projectList = Globals._globalController.harvestManager._PROJECTLIST;
            Globals._globalController.fetchAndPopulateForDate(currentDateForWindow);    
    }

    public void addHarvestEntrytoView(Harvest_TimeSheetEntry entry)
    {
        try
        {
            listBox1.Items.Add(entry);
        }
        catch (Exception)
        { }
    }

    public void addHarvestEntrytoView(List<Harvest_TimeSheetEntry> entry)
    {
        foreach (Harvest_TimeSheetEntry x in entry)
            listBox1.Items.Add(x);
    }

    private void BackButton_Click(object sender, RoutedEventArgs e)
    {
            this.Hide();
            Globals._globalController.getMonthViewWindow.Show();
    }

    private void StartButton_Click(object sender, RoutedEventArgs e)
    {
        Globals._globalController.win32Manager.startTimer();
    }

    private void StopButton_Click_1(object sender, RoutedEventArgs e)
    {
        Globals._globalController.win32Manager.stopTimer();

    }

    private void SyncEntry_Click(object sender, RoutedEventArgs e)
    {
        //Submit All unsynced Entries
    }

    private void ListBoxItem_MouseDoubleClick(object sender, RoutedEventArgs e)
    {
        //Submit clicked Entry
        Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)sender;

        if (!entryToPost.isSynced)
        {
            //Check if something is selected in selectedProjectItem For that item


            if (entryToPost.ProjectNameBinding == "Select Project")
                MessageBox.Show("Please Select a Project for the Entry");
            else
                Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
        }
        else
        {
            //Already synced.. Make a noise or something
            MessageBox.Show("Already Synced;TODO Play a Sound Instead");
        }
    }
}
3
I believe the error "BindingExpression path error: 'projectList' property not found on 'object' " is complaining because projectList is currently not a property (no defined accessors).Chris
You also have a binding error for selectedProjectid. I believe that's because you forgot the ElementName=MainWin part on this bindingKevin Gosse
@Chris You may post your comment as an answer.Clemens
@Chris Thanks. But still binding error for selectedProjectid is left.Dinesh
I defined accessors for ElementName as well. because that is also part of binding.Dinesh

3 Answers

8
votes

like Chris mentioned, binding just work with public poperties. so you have to do at least:

 public List<Harvest_Project> projectList {get;set;}

your xaml for itemssource {Binding Path=projectList, ElementName=MainWin} means that your element MainWin has a Property projectList - i think thats not what you wanted.

EDIT: if you have any binding errors there are just 2 simple steps to resolve this

  1. check your DataContext
  2. check your binding path

at runtime you can use Snoop for this task.

for your selectedProjectid binding: you expect a DataContext with a public property selectedProjectid. if this it not the case you should check your code

6
votes

I would like to add to the above. I also had a Binding error.

    System.Windows.Data Error: BindingExpression path error: 'GeneralInformationTopicSectionItemStyles' property not found on 'GeneralInformation.Resources.LocalizationFiles' 'GeneralInformation.Resources.LocalizationFiles' (HashCode=7180698). BindingExpression: Path='GeneralInformationTopicSectionItemStyles.ItemNameTextWithoutSectionFontWeight' DataItem='GeneralInformation.Resources.LocalizationFiles' (HashCode=7180698); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'FontWeight' (type 'System.Windows.FontWeight')

And I could not figure out what the problem was. In my LocalizationFiles class I had the member.

    public static object GeneralInformationTopicSectionItemStyles;

After reading the above post, I changed this from a member to a property.

    public static object GeneralInformationTopicSectionItemStyles
    {
        get;
        set;
    }

And Voila! It worked like a charm.

MM

2
votes

This is essentially what blindmeis has already said, but I'll expand on my comment =D

Your error: "BindingExpression path error: 'projectList' property not found on 'object' " is a result of the missing property definition on your object; in this case projectList is currently not a property (no defined accessors).

You should also have another public property to hold your selected item (or value depending on what you're interested in.

public partial class DayView : MetroWindow
{
    public List<Harvest_Project> projectList { get; set; }
    public Harvest_Project selectedProject  { get; set; }

    // Your other code lives here.
}

Binding along these lines (with all your element names/contexts as required):

<ComboBox x:Name="ComboBox2" 
     ItemsSource="{Binding Path=projectList}"
     SelectedItem="{Binding Path=selectedProject}" Mode=TwoWay}" />

You'll have to implement iNotifyPropertyChanged if you wish to be notified of the selectedItem changes, but that's another topic =D