RadGridView inside Header Binding not set when expander collapsed.

4 posts, 0 answers
  1. Muds
    Muds avatar
    17 posts
    Member since:
    Jan 2015

    Posted 09 Feb 2015 Link to this post

    Hi All

    I have an issue with binding of a control which is inside header of a RadGridView which is in an Expander where Expander.IsExpanded
    =False


    <telerik:GridViewDataColumn.Header>
         <CheckBox IsChecked="{Binding DataContext.MyProperty, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>
    </telerik:GridViewDataColumn.Header>


    When I set IsExpanded=True, binding to checkbox starts working as normal.

    As a workaround I did this in code behind ..

    Private Sub CheckBox_OnLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
                Dim chk As CheckBox = sender
                chk.DataContext = DataContext
                Dim b As Binding = New Binding("MyProperty")            
                chk.SetBinding(CheckBox.IsCheckedProperty, b)         
    End Sub

    This works, but I don't want to do this as its not great coding practice and any I will have to repeat this like 1000s time that I don't wanna do..

    please tell me what options do I have here.

    Thanks
    M. 






  2. Boris
    Admin
    Boris avatar
    276 posts

    Posted 12 Feb 2015 Link to this post

    Hello Muds,

    A possible way to go about this is to define your ViewModel as a static resource:

    <Window x:Class="RadGridView_WPF_NoXAML_56.MainWindow"
            xmlns:my="clr-namespace:RadGridView_WPF_NoXAML_56"
            Title="MainWindow" Height="700" Width="700">
        <Window.Resources>
            <my:MyViewModel x:Key="MyViewModel"/>
        </Window.Resources>
        <Grid DataContext="{StaticResource MyViewModel}">
            ...
        </Grid>
    </Window>

    Then you can bind the IsChecked property of the CheckBox element to a property in your ViewModel (in this example this is the IsCheckBoxChecked property) like so:

    <telerik:GridViewDataColumn.Header>
            <CheckBox IsChecked="{Binding IsCheckBoxChecked, Source={StaticResource MyViewModel}}"/>
    </telerik:GridViewDataColumn.Header>


    I hope this helps.

    Regards,
    Boris
    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. Muds
    Muds avatar
    17 posts
    Member since:
    Jan 2015

    Posted 12 Feb 2015 in reply to Boris Link to this post

    Boris

    Thanks for your reply, but view model in most cases has no default constructor hence it will not be possible to use this as a solution.

  4. Boris
    Admin
    Boris avatar
    276 posts

    Posted 13 Feb 2015 Link to this post

    Hello Muds,

    If you are keen on using a ViewModel without a default constructor you can define it in code-behind and then add it to the Resource collection of the Window:

    public partial class MainWindow : Window
        {
            MyViewModel vm;
            public MainWindow()
            {
                vm = new MyViewModel("some parameters for my constructor");
     
                this.Resources.Add("MyViewModel2", vm);
         
                InitializeComponent();
                 
            }
        }

    Then you will be able to use it as a static resource as demonstrated in my previous reply.

    I hope this helps.

    Regards,
    Boris
    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.

     
Back to Top