2

Here is my problem, I have the following data structure:

public class Job : INotifyPropertyChanged {
    public StateEnum State {
        get { return this.state; }

        private set {
            this.state = value;
            this.OnPropertyChanged();
        }
    }
}

public class MainWindow : Window, INotifyPropertyChanged 
    public List<Job> Jobs {
        get { return this.jobs; }

        private set {
            this.jobs = value;
            this.OnPropertyChanged();
        }
    }
}

I want to display a global state summary of the jobs in the main window.

I first tried to make a data binding on the Jobs list, then use a custom IValueConverter to display the global state. Problem: It is not refreshed when the job states change (since it is bind to the collection and not the states).

ProgressState="{Binding Jobs, Converter={StaticResource JobsToProgressState}, ElementName=MainWindow}"

So I was trying to find a solution where I can bind all the nested properties of the jobs to a IMultiValueConverter. I did not find any syntax to make this work.

Is it possible to do something like that?

EDIT: I want to do something like

ProgressState="{Binding Jobs[*].State, Converter={StaticResource JobsToProgressState}, ElementName=MainWindow}"

And retrieve an array containing all job states (StateEnum[]) in the JobsToProgressState converter.

8
  • 2
    State and Jobs are fields, not properties. Data binding works with public properties only. Please post the relevant parts of your exact code here to avoid confusion. Commented Jun 5, 2018 at 17:06
  • As @Clemens mentioned that you have to define the field as property, also, you should implement INotifyPropertyChanged interface Commented Jun 5, 2018 at 17:08
  • Sorry, I will make it explicit in my sample. But these are public DependencyProperties. Commented Jun 5, 2018 at 17:09
  • 1
    If Jobs collection is changing then you should implement as ObservableCollection instead of List Commented Jun 5, 2018 at 17:11
  • The job collection is not changing. The state of the jobs are changing. Commented Jun 5, 2018 at 17:13

1 Answer 1

1

The problem is that OnPropertyChanged is not fired when a record of an IList changes. You need to delegate the OnPropertyChanged of the Job up to your Jobs-List.

This rough implementation will do what you want.

public class Job : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged; 
    public StateEnum State {
        get { return this.state; }

        private set {
            this.state = value;
            this.OnPropertyChanged();
        }
    }
}

public class MainWindow : Window, INotifyPropertyChanged 
    public List<Job> Jobs {
        get { return this.jobs; }

        private set {
            this.jobs = value;

            foreach(var job in this.jobs)
            {
                job.PropertyChanged += job_PropertyChanged;
            }
        }
    }

    private void job_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        this.OnPropertyChanged("Jobs");
    }
}

Don't forget to unwire your event registrations when you don't need them any more.

Sign up to request clarification or add additional context in comments.

1 Comment

Neither a method nor an event is "thrown". A method is called, and an event is raised (or sometimes fired).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.