This is a migrated thread and some comments may be shown as answers.

AppointmentFilter stops working when DataContext is set in code behind

3 Answers 112 Views
ScheduleView
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 13 May 2014, 11:59 PM
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!

3 Answers, 1 is accepted

Sort by
0
George
Telerik team
answered on 15 May 2014, 02:09 PM
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.
 
0
Brian
Top achievements
Rank 1
answered on 15 May 2014, 05:34 PM
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!
0
George
Telerik team
answered on 16 May 2014, 06:37 AM
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.
 
Tags
ScheduleView
Asked by
Brian
Top achievements
Rank 1
Answers by
George
Telerik team
Brian
Top achievements
Rank 1
Share this question
or