开发者

How to bind a dependency property to a control in WPF?

开发者 https://www.devze.com 2023-02-20 08:46 出处:网络
I have created a Day class: public class Day { public int DayOfMonth { get { return dayOfMonth; } } public List<Entry> CalendarDayItems

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}" />
        <Item开发者_开发技巧sControl 
            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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消