AppointmentFilter stops working when DataContext is set in code behind

4 posts, 0 answers
  1. Brian
    Brian avatar
    12 posts
    Member since:
    Feb 2014

    Posted 13 May 2014 Link to this post

    I'm using a RadScheduleView with some combobox dropdowns to set filter options. Here's how I set this up (excess code has been removed):

    <UserControl x:Class="CalendarsContainer"
        <Grid x:Name="ScheduleGrid">
            <Grid.DataContext>
                <viewModels:CalendarViewModel />
            </Grid.DataContext>
            <telerik:RadComboBox Grid.Row="0" SelectedValue="{Binding TaskFilter, Mode=TwoWay}"/>
            <telerik:RadScheduleView Grid.Row="1" AppointmentsSource="{Binding Appointments}">
                <telerik:RadScheduleView.ViewDefinitions>
                    <telerik:MonthViewDefinition AppointmentFilter="{Binding AppointmentFilter}" />
                </telerik:RadScheduleView.ViewDefinitions>
            </telerik:RadScheduleView>
        </Grid>
    </UserControl>


    When I use this, I get a binding error after it's first initialized:

    Get System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=AppointmentFilter; DataItem='CalendarViewModel' (HashCode=6511533); target element is 'MonthViewDefinition' (HashCode=63187382); target property is 'AppointmentFilter' (type 'Predicate`1')

    But other than that, everything works as I expect. My appointments are displayed as per my filter settings. As I change my dropdown, the AppointmentFilter gets called for every appointment and the visible appointments are changed to match the filter.

    But, if I remove setting of the DataContext from the xaml:

    <UserControl x:Class="CalendarsContainer"
        <Grid x:Name="ScheduleGrid">
            <telerik:RadComboBox Grid.Row="0" SelectedValue="{Binding TaskFilter, Mode=TwoWay}"/>
            <telerik:RadScheduleView Grid.Row="1" AppointmentsSource="{Binding Appointments}">
                <telerik:RadScheduleView.ViewDefinitions>
                    <telerik:MonthViewDefinition AppointmentFilter="{Binding AppointmentFilter}" />
                </telerik:RadScheduleView.ViewDefinitions>
            </telerik:RadScheduleView>
        </Grid>
    </UserControl>

    and move it to the code behind:

    public CalendarsContainer()
    {
        InitializeComponent();
        ScheduleGrid.DataContext = new CalendarViewModel();
    }

    Now, instead of seeing the "governing FrameworkElement" binding error first, the AppointmentFilter is called for each appointment right away, but then I get the binding error at the end:

    System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=AppointmentFilter; DataItem=null; target element is 'MonthViewDefinition' (HashCode=32578312); target property is 'AppointmentFilter' (type 'Predicate`1')

    At this point, ALL appointments are displayed, not just the ones that meet the filter requirements.

    Even worse is that, as I change the dropdown, the AppointmentFilter gets called for every appointment as I expect, but the visible appointments don't change!! For some reason, the AppointmentFilter is called, but then completely ignored, so all appointments are displayed.

    As far as I can tell, the governing FrameworkElement binding is known WPF behavior. What I don't understand is why RadScheduleView doesn't fully recover from this. Since the AppointmentFilter is being called, it seems to partially recover, but then the results of the filtering are ignored.

    How can I fix this problem?

    Thank you!
  2. George
    Admin
    George avatar
    1332 posts

    Posted 15 May 2014 Link to this post

    Hello Brian,

    We will try to setup a sample project and reproduce the described problem on our side with the provided code snippets. Meanwhile, you can move setting the DataContext before the InitializeComponent?

    Regards,
    George
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Brian
    Brian avatar
    12 posts
    Member since:
    Feb 2014

    Posted 15 May 2014 in reply to George Link to this post

    At first I thought I couldn't do that since my ScheduleGrid hasn't been initialized until after. But then I realized I can set the DataContext on my UserControl instead of just on the Grid. Now I have:

    public CSCalendarsContainer(Window parentWindow)
    {
        DataContext = new CalendarViewModel(new CreateCalendarDialogService(parentWindow));
        InitializeComponent();
    }

    And this works as expected.

    Thank you!
  5. George
    Admin
    George avatar
    1332 posts

    Posted 16 May 2014 Link to this post

    Hello Brian,

    I am glad that the issue you were facing is now resolved. Please do not hesitate to contact us if you have any additional questions.

    Regards,
    George
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top