Cannot find Datacontext (Viewdefinitions)

2 posts, 0 answers
  1. Vikas
    Vikas avatar
    64 posts
    Member since:
    May 2014

    Posted 25 Oct 2014 Link to this post

    I am having problem in binding to the Datacontext at viewdefinition level, It works if i create my viewmodel in xaml and set the DataContext of the grid using staticresource, but if i set the datacontext to the window in code behind it does not work

                <schedule:RadScheduleView.ViewDefinitions>
                    <schedule:DayViewDefinition GroupFilter="{Binding GroupFilter2}"/>
                    <schedule:WeekViewDefinition GroupFilter="{Binding GroupFilter2}"/>
                    <schedule:MonthViewDefinition GroupFilter="{Binding GroupFilter2}"/>
                </schedule:RadScheduleView.ViewDefinitions>

    ----------- this does not work, does no bind to Group Filter -----------------
    public MainWindow()
            {
                InitializeComponent();
                this.DataContext = new ViewModel();
            }


    ----------- this works -------------------

    <Window.Resources>
            <local:ViewModel x:Key="ViewModel" AppointmentsSource="/Schedule;component/Appointments.xaml" />
    <Grid x:Name="mainGrid" DataContext="{StaticResource ViewModel"> 
  2. Kalin
    Admin
    Kalin avatar
    1207 posts

    Posted 27 Oct 2014 Link to this post

    Hi Vikas,

    In order to get the binding working correctly you would need to either set the DataContext in XAML or above the InitializeComponent in the constructor of the MainWindow:

    public MainWindow()
    {   
        this.DataContext = new ViewModel();
        InitializeComponent();
    }

    Hope this helps.

    Regards,
    Kalin
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top