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

RadGridView inside Header Binding not set when expander collapsed.

3 Answers 136 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Muds
Top achievements
Rank 1
Muds asked on 09 Feb 2015, 12:47 PM
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. 






3 Answers, 1 is accepted

Sort by
0
Boris
Telerik team
answered on 12 Feb 2015, 11:53 AM
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.

 
0
Muds
Top achievements
Rank 1
answered on 12 Feb 2015, 02:24 PM
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.

0
Boris
Telerik team
answered on 13 Feb 2015, 12:01 PM
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.

 
Tags
GridView
Asked by
Muds
Top achievements
Rank 1
Answers by
Boris
Telerik team
Muds
Top achievements
Rank 1
Share this question
or