1

I have created a Day class:

public class Day { public int DayOfMonth { get { return dayOfMonth; } } public List<Entry> CalendarDayItems { get { return calendarDayItems; } set { calendarDayItems = value; } } private DateTime date; private int dayOfMonth; private List<Entry> calendarDayItems; public Day(DateTime date, List<Entry> entries) { this.date = date; this.dayOfMonth = date.Day; this.calendarDayItems = entries; } } 

Next I have created a WPF UserControl for which I want to bind the collection of days to the ItemsControl. I have created a dependency property ObservableCollection<Day> Days which is bound to the ItemsControl. Here's XAML:

<UserControl ... Name="CalendarMonthViewControl"> ... <ItemsControl ItemsSource="{Binding ElementName=CalendarMonthViewControl, Path=Days}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Rows="6" Columns="7" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate DataType="Day"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <!-- The following two bindings don't work --> <TextBlock Grid.Column="0" Text="{Binding Path=DayOfMonth}" /> <ItemsControl Grid.Column="1" ItemsSource="{Binding Path=CalendarDayItems}"> </ItemsControl> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> ... 

I have a couple of questions:

  1. Am I using the proper way to bind a dependecy property to the ItemsControl, i.e. is it recommended to name the control and then to reference it as a binding source?
  2. The main problem is that the TextBlock and the second ItemsControl won't bind to DayOfMonth and CalendarDayItems properties of the Day class, respectively.
8
  • @Boris When you say "won't bind", do you mean you don't see the initial values of these properties (and the output window of VS shows binding errors) or that changes to these properties aren't shown? Commented Mar 28, 2011 at 2:04
  • @grantnz I meant when I run the application, the TextBlock and the ItemsControl are not showing any data, DayOfMonth value and the list of CalendarDayItems are not showing. They are just blank, but there are no errors or exceptions. Commented Mar 28, 2011 at 2:10
  • It is as if the binding is trying to find those properties on the control and not for the Day items in the collection... Commented Mar 28, 2011 at 2:11
  • 1
    If you run your app in the debugger, are there any binding errors in the Output window? Commented Mar 28, 2011 at 2:41
  • 1
    @Andy, the output window showed the errors in the debugger. This lead me to finding the answer and making the binding work. Please, post your comment as an answer, so I can mark it correct. Commented Mar 28, 2011 at 8:52

2 Answers 2

1

If you run your app in the debugger, any errors with bindings will be shown in the Output window. This can be used to figure out why a binding isn't working.

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

1 Comment

In the output window, I found that the DayOfMonth and MonthEntries were non-existing properties. Once I've checked the Day class I noticed that I've set internal accessors to these properties. Changing them to public resolved the binding issue. Thanks Andy for pointing me to the right direction.
0

There are some good suggestions for resolving Q2, but for Q1 I would suggest that you set the DataContext for the UserControl and then in the ItemsControl you can just use ItemsSource="{Binding Path=Days}".

This allows you to easily replace the DataContext with another, for example to do simple testing of your control. It also means that if Days or other properties are used for other controls in your UserControl, you don't need to repeat the ElementName tags.

This mechanism is commonly used with the Model-View-ViewModel (MVVM) design pattern (see MSDN), where the ViewModel is the DataContext for the View.

In your example, you could simply set the DataContext either directly in the XAML, using something like:

<UserControl DataContext="{Binding Path=CalendarMonthViewControl}}" ... /> 

Or you could set in it the code behind file.

4 Comments

Disagree, there is no need to put a MVVM pattern in every hole. Simple controls should be implemented in regular way, MVVM will not make it easier, probably will make it worse. OP's usage of Dependency property is correct. Also your answer seems to unclear to me, you're not answering second question and for first question you have only couple of words about what should be changed. You do not say how and who should set the DataContext.
@Snowbear, would you agree that I am binding dependency property Days to the ItemsControl in a recommendable manner? This binding works, but I wish to improve my skills, so maybe the usage of DataContext is a better solution?
@Boris, IMO, your way to bind it is correct, there are other slightly different options but they won't give you much benefits. If I would write such a control I would use the approach you've chosen, I do not see anything better. P.S. Sorry for spamming in answers.
@Snowbear, I'm not suggesting that this control necessarily needs to use MVVM, but it is an example of setting the DataContext. I'll update my answer to clarify.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.