ComboBox within RadGridView's Header Binding is Breaking After switching tabs
I have several Telerik RadGridViews within a TabControl.
Each of these RadGridViews has a column with CheckBoxes in. The header for this column is a CheckBox. When the user clicks the CheckBox in the header, CheckBoxes within the column are checked or unchecked accordingly.
The checking/unchecking of the CheckBoxes works for the RadGridView in the tab that is, by default, selected.
The checking/unchecking does not work in any of the RadGridViews that are in tabs that were not selected by default. When you return to the tab that selected by default, it still works.
I took a copy of the CheckBox that is in the header and placed it above the RadGridView and it works fine after switching tabs... If you check/uncheck this CheckBox it will check/uncheck all of the CheckBoxes in the Grid...but the CheckBox in the Header doesn't work.
It's almost as if the Binding for the CheckBox in the header of the Grid not applied if the Grid is not visible when the page loads....
I was wondering if anyone knows how to get around this problem.
<TablControl>
<TabItem Header="Items">
<telerik:RadGridView x:Name="GridA" AutoGenerateColumns="False" ItemsSource="{Binding MyItems}">
<telerik:RadGridView.Columns>
<telerik:GridViewBoundColumnBase>
<telerik:GridViewBoundColumnBase.Header>
<CheckBox IsChecked="{Binding ElementName=TheElementBoundToTheViewModel, Path=DataContext.SelectAllMyItems, Mode=TwoWay}" />
</telerik:GridViewBoundColumnBase.Header>
<telerik:GridViewBoundColumnBase.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}"></CheckBox>
</DataTemplate>
</telerik:GridViewBoundColumnBase.CellTemplate>
</telerik:GridViewBoundColumnBase>
<!-- ...... other stuff ..... -->
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</TabItem>
<TabItem Header="ItemsB">
<telerik:RadGridView x:Name="GridB" AutoGenerateColumns="False" ItemsSource="{Binding MyItemsB}">
<telerik:RadGridView.Columns>
<telerik:GridViewBoundColumnBase>
<telerik:GridViewBoundColumnBase.Header>
<CheckBox IsChecked="{Binding ElementName=TheElementBoundToTheViewModel, Path=DataContext.SelectAllMyItems, Mode=TwoWay}" />
</telerik:GridViewBoundColumnBase.Header>
<telerik:GridViewBoundColumnBase.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}"></CheckBox>
</DataTemplate>
</telerik:GridViewBoundColumnBase.CellTemplate>
</telerik:GridViewBoundColumnBase>
<!-- ...... other stuff ..... -->
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</TabItem>
</TabControl>
This is my ViewModel:
Public Class MyItemsVM
Inherits ViewModelBase
Private _selectAllMyItems As Boolean
Public Property MyItems As ObservableCollection(Of MyItemSelectionVM)
Public Property SelectAllMyItems As Boolean
Get
Return _selectAllMyItems
End Get
Set(ByVal value As Boolean)
_selectAllMyItems = value
For Each ivm In MyItems
ivm.IsSelected = _selectAllMyItems
Next
MyBase.RaisePropertyChangedEvent(Me, SelectAllMyItemsChanged)
End Set
End Property
Private _selectAllMyItemsB As Boolean
Public Property MyItemsB As ObservableCollection(Of MyItemSel开发者_高级运维ectionVM)
Public Property SelectAllMyItemsB As Boolean
Get
Return _selectAllMyItemsB
End Get
Set(ByVal value As Boolean)
_selectAllMyItemsB = value
For Each ivm In MyItemsB
ivm.IsSelected = _selectAllMyItemsB
Next
MyBase.RaisePropertyChangedEvent(Me, SelectAllMyItemsBChanged)
End Set
End Property
Public Sub New(ByVal items As MyItemList,ByVal itemsB As MyItemList)
MyItems = New ObservableCollection(Of MyItemSelectionVM)
For Each item In items
MyItems.Add(New MyItemSelectionVM(item))
Next
MyItemsB = New ObservableCollection(Of MyItemSelectionVM)
For Each item In itemsB
MyItemsB.Add(New MyItemSelectionVM(item))
Next
End Sub
End Class
Public Class MyItemSelectionVM
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private _isSelected As Boolean
Private _item As MyItem
Public Property MyItem As MyItem
Get
Return _item
End Get
Set(ByVal value As MyItem)
_item = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("MyItem"))
End Set
End Property
Public Property IsSelected As Boolean
Get
Return _isSelected
End Get
Set(ByVal value As Boolean)
_isSelected = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IsSelected"))
End Set
End Property
Public Sub New(ByVal theItem As MyItem)
_item = theItem
End Sub
End Class
Thanks for your help
-Frinny
I solved the problem by creating a Static Resource for the View Model. Binding to the Static Resource instead of an element's DataContext fixed the problem.
精彩评论