RadGridView的Header Binding中的ComboBox在切换选项卡后中断

时间:2011-05-17 20:35:56

标签: wpf binding telerik

我在TabControl中有几个Telerik RadGridViews。

这些RadGridViews中的每一个都有一个包含CheckBoxes的列。此列的标题是CheckBox。当用户单击标题中的CheckBox时,将相应地选中或取消选中列中的CheckBoxes。

CheckBoxes的检查/取消选中适用于默认情况下选中的选项卡中的RadGridView。

检查/取消选中在默认情况下未选中的选项卡中的任何RadGridView中都不起作用。当您返回默认选择的选项卡时,它仍然有效。

我拿了标题中的CheckBox副本并将其放在RadGridView上方,切换标签后工作正常......如果选中/取消选中此CheckBox,它将检查/取消选中网格中的所有CheckBox ...但是Header中的CheckBox不起作用。

如果在页面加载时网格不可见,那么网格标题中的CheckBox的Binding几乎就不会被应用....

我想知道是否有人知道如何解决这个问题。

<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>

这是我的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 MyItemSelectionVM)
  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

感谢您的帮助

-Frinny

1 个答案:

答案 0 :(得分:0)

我通过为视图模型创建静态资源来解决问题。绑定到静态资源而不是元素的DataContext修复了问题。